laravel5.8开发笔记:路由
laravel框架,必须先设置路由,才可以访问内部的控制器部分。
路由文件:routes/.
基本路由
Route::get('/user', 'UserController@index');
可用的路由方法
Route::get($uri, $callback); Route::post($uri, $callback); Route::put($uri, $callback); Route::patch($uri, $callback); Route::delete($uri, $callback); Route::options($uri, $callback);
重定向路由
// 重定向 访问http://claravel57.com/da 会跳转到http://claravel57.com/there
Route::redirect('da','there',301);
Route::redirect('db','',301);
视图路由
Route::view('/welcome', 'welcome');
Route::view('/welcome', 'welcome', ['name' => 'Taylor']);
路由参数
Route::get('user/{id}', function ($id) {
return'User '.$id;
});
Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) {
路由传参数
// 必选传参数
Route::get('user/{id}', function ($id) {
return'User '.$id;
});
// 可选传参数,由问号决定
Route::get('user/{id?}', function ($id) {
return'User '.$id;
});
还有很多的路由方式,可以查阅官方手册
路由在实际项目中应用
场景一:单模块路由(Index)
Route::get('/user','UserController@index');
场景二:多个模块路由 (Admin,Index,Api),采用群组路由
/*
* api路由群组
* 访问格式 http://localhost/xxx/
* @param prefix 路由别名
* @param namespace 别名对应的命名空间(Admin,Index,Api)
* */// 路由群组 访问:http://localhost/admin/goods
Route::group(['prefix' => 'admin', 'namespace' => 'Admin'] ,function () {
//Admin模块
//Route::get('/控制名/方法','IndexController@index');
Route::get('/goods/','IndexController@index');
});
// 路由群组 访问:http://localhost/index/index
Route::group(['prefix' => 'index', 'namespace' => 'Index'] ,function () {
//Index模块
Route::get('/index','IndexController@index');
});
// Api模块路由群组 访问:http://localhost/api/info
Route::group(['prefix' => 'api', 'namespace' => 'Api'] ,function () {
//api模块
Route::get('/info/','IndexController@index');
});
如果不使用群组路由,路由格式
Route::get('/admin/goods/index','Admin\IndexController@index');
Route::get('/Index/index/index','Admin\IndexController@index');
Route::get('/Api/info/index','Admin\IndexController@index');
使用群组路由的好处:每个模块下的路由更加美观,敲打的代码更少;多模块部署推荐使用群组路由
场景三:路由与控制器之间的传参
单个参数:Route::get('user/{id}', function ($id) { return 'User '.$id; });
多个参数:Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) {
// 页面Get传参数
http://xx.com?id=1&name=MR.zhou&age=18&sex=1
// 路由
Route::get('/Api/info/index','Admin\IndexController@index');
// 控制器public function index(){
$param = Request()->input();
$name = $param['name']
}
场景四:路由引用中间件(登陆检测)
Route::get('/index','IndexController@index')->middleware('goods');
场景五:路由回滚
// 路由回滚 定义一些不存在的页面Route::fallback(function () {
return'404';
});
容易报错
除了默认路由,其他路由模式无法访问
server {
listen 80;
server_name l58.com;
root "D:/phpStudy/PHPTutorial/WWW/composer/l58/public";
location / {
index index.html index.htm index.php;
#autoindex on;
try_files $uri $uri/ /$query_string; // 路由访问失效,加上此处 }
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}
提示类不存在:Class App\Http\Controllers\Index/IndexController does not exist
Route::get('/Api/info/index','Admin/IndexController@index'); // 错误
Route::get('/Api/info/index','Admin\IndexController@index'); // 正确
THE END




