laravel5.8开发笔记:路由

2019-05-1715:04:28后端程序开发Comments3,676 views字数 2961阅读模式

laravel框架,必须先设置路由,才可以访问内部的控制器部分。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/12548.html

路由文件:routes/.文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/12548.html

基本路由文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/12548.html

Route::get('/user', 'UserController@index');

可用的路由方法文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/12548.html

Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);

重定向路由文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/12548.html

// 重定向  访问http://claravel57.com/da 会跳转到http://claravel57.com/there
Route::redirect('da','there',301);
Route::redirect('db','',301);

视图路由文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/12548.html

Route::view('/welcome', 'welcome');
Route::view('/welcome', 'welcome', ['name' => 'Taylor']);

路由参数文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/12548.html

Route::get('user/{id}', function ($id) {
    return'User '.$id;
});
Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) { 

路由传参数文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/12548.html

// 必选传参数 
Route::get('user/{id}', function ($id) {
    return'User '.$id;
});
// 可选传参数,由问号决定
Route::get('user/{id?}', function ($id) {
    return'User '.$id;
});

还有很多的路由方式,可以查阅官方手册文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/12548.html

路由在实际项目中应用文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/12548.html

场景一:单模块路由(Index)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/12548.html

Route::get('/user','UserController@index');

场景二:多个模块路由 (Admin,Index,Api),采用群组路由文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/12548.html

/*
 * 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');
});

如果不使用群组路由,路由格式文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/12548.html

Route::get('/admin/goods/index','Admin\IndexController@index');
Route::get('/Index/index/index','Admin\IndexController@index');
Route::get('/Api/info/index','Admin\IndexController@index');

使用群组路由的好处:每个模块下的路由更加美观,敲打的代码更少;多模块部署推荐使用群组路由文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/12548.html

场景三:路由与控制器之间的传参文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/12548.html

单个参数:Route::get('user/{id}', function ($id) { return 'User '.$id; });文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/12548.html

多个参数:Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) {文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/12548.html

// 页面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']
}

场景四:路由引用中间件(登陆检测)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/12548.html

Route::get('/index','IndexController@index')->middleware('goods');

场景五:路由回滚文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/12548.html

// 路由回滚  定义一些不存在的页面Route::fallback(function () {
    return'404';
});

容易报错文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/12548.html

除了默认路由,其他路由模式无法访问文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/12548.html

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文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/12548.html

Route::get('/Api/info/index','Admin/IndexController@index');   // 错误
Route::get('/Api/info/index','Admin\IndexController@index');  // 正确
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/12548.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/12548.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定