laravel5.8笔记四:路由

laravel框架,必须先设置路由,才可以访问内部的控制器部分。

 路由文件:routes/web.php.

 

基本路由

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','http://www.baidu.com',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) { // });    // 此方法有弊端(非常多的参数时候,路由会写的很长),不推荐使用,可以使用一下传参数

路由:Route::get('user/'function ($idreturn 'User '.$id});

// 页面Get传参数
http://xx.com?id=1&name=MR.zhou&age=18&sex=1
// 路由web.php
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/ /index.php?$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');  // 正确

 

转载于:https://www.cnblogs.com/wesky/p/10443206.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值