一、路由 php artisan route:list
routes/web.php
Route::get('/lin', function () {
echo "nihao lin";
});
//1.路由参数 ,可选多个问号{id?}
//http://www.laravel02.com/user/111
Route::get('user/{id?}', function ($id = 0) {
echo "nihao".$id;
});
//1.1通过?形式来传递参数,不需要写路由
//http://www.laravel02.com/test?id=111
Route::any('test', function () {
echo "nihao".$_GET['id'];
});
//2.路由类型
Route::match(['get', 'post'], '/purchase', function(){});
Route::any('foo',function(){});
//3.路由别名
Route::any('test2', function () {
echo "nihao".$_GET['id'];
})->name('新名字');
//调用路由:route('新名字');
//4.路由群组 admin
Route::group(['prefix'=>'admin'],function(){
Route::get('test1', function () {
//匹配 /admin/test1
echo "/admin/test1";
});
Route::any('/test2', function () {
//匹配 /admin/test2
});
});
二、php artisan make:controller 控制器名 (大驼峰)+Controller
1.控制器生成:
php artisan make:controller TestController
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;//命名空间的三元素:常量,方法和类
class TestController extends Controller
{
public function test1(){
phpinfo();
}
}
2.控制器路由:
使用路由规则调用控制器下的方法,非回调函数
“控制器类名@方法名”
// 实战测试 Route::get('/home/test/test1','TestController@test1'); 上面调试未通过,下面通过: Route::get('/home/test/test1',[TestController::class,'test1']); http://www.laravel02.com/home/test/test1
3. 分目录管理, 命令里增加目录名即可:
E:phpStudyPHPTutorialWWWlaravel02>php artisan make:controller Home/IndexController
E:phpStudyPHPTutorialWWWlaravel02>php artisan make:controller Admin/IndexController
//home目录 index 类的index方法
Route::get('/home/index/index','HomeIndexController@index');
Route::get('/admin/index/index','AdminIndexController@index');
http://www.laravel02.com/home/index/index
http://www.laravel02.com/admin/index/index
4. 接收用户输入
接收用户输入的类:IlluminateSupportFacadesInput
Facades 是类的一个接口实现,算静态方法
Input::get();
Input::all();
input::only([])
input::except([])
简化 use IlluminateSupportFacadesInput ,给它添加别名
测试:
Route::get('/home/test/test2','TestController@test2');
http://www.laravel02.com/home/test/test2?ddd=aaaa&id=112&name=zhangsan <?php namespace AppHttpControllers; use IlluminateHttpRequest;//命名空间的三元素:常量,方法和类 //use IlluminateSupportFacadesInput; use Input; class TestController extends Controller { public function test1(){ phpinfo(); } //测试input public function test2(){ //获取一个值,如果没值默认第二个参数 echo Input::get('id',"10086"); //获取全部(数组格式) $all = Input::all(); //dump + die ,后续代码不会执行 // dd($all); //获取指定信息(字符串格式) // dd(Input::get('name')); //获取指定几个key(数组格式) // dd(Input::only(['id','name'])); //获取指定几个key之外的值(数组格式) // dd(Input::except(['name'])); //判断key是否存在(boole) dd(Input::has('name')); } }
输出: 112
array:1 [▼
"id" => "112"
]
