zoukankan      html  css  js  c++  java
  • 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','AdminIndexController@index');
    Route::get('/Index/index/index','AdminIndexController@index');
    Route::get('/Api/info/index','AdminIndexController@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','AdminIndexController@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 AppHttpControllersIndex/IndexController does not exist

    Route::get('/Api/info/index','Admin/IndexController@index');   // 错误
    Route::get('/Api/info/index','AdminIndexController@index');  // 正确
  • 相关阅读:
    SPOJ ROCK
    CodeForces 4C
    实现与研华PCI采集卡通讯
    C#常用数据结构
    使用USBCAN通讯
    COMMTIMEOUTS读写串行口超时
    Multithreading With C# Cookbook---Chapter5---使用C#6.0
    Multithreading With C# Cookbook---Chapter4---使用任务并行库
    Multithreading With C# Cookbook---Chapter3---线程池
    Multithreading With C# Cookbook---Chapter2---线程同步
  • 原文地址:https://www.cnblogs.com/wesky/p/10443206.html
Copyright © 2011-2022 走看看