技术笔记
laravel7路由学习笔记
 moons   2020-08-06 17:53:06   129
专栏分类: PHP
    <?php
    
    /*
    |--------------------------------------------------------------------------
    | Web Routes
    |--------------------------------------------------------------------------
    |
    | Here is where you can register web routes for your application. These
    | routes are loaded by the RouteServiceProvider within a group which
    | contains the "web" middleware group. Now create something great!
    |
    */
    /*访问根目录*/
    
    Route::get('/', function () {
        return 'Hello';
    });
    // 基础路由
    Route::get('index', function () {
        return 'Hello Index';
    });
    //多请求路由
    Route::match(['get', 'post'], 'test1', function () {
        return 'get/post 多请求路由';
    });
    Route::any('test2', function () {
        return 'any:多请求路由';
    });
    // 路由参数
    /*
    Route::get('user/{id}',function ($id){
       return 'User-id-'.$id;
    });
    Route::get('user/{name?}',function ($name=null){
        return 'User-name-'.$name;
    });
    Route::get('user/{name?}',function ($name='参数'){
        return 'User-name-'.$name;
    });
    // 加正则表达式
    Route::get('user/{name?}',function ($name='参数'){
        return 'User-name-'.$name;
    })->where('name','[A-Za-z]+');
    // 多参数
    Route::get('user/{id}/{name?}', function ($id, $name = '参数') {
        return 'User-name-' . $name . ', User-id-' . $id;
    })->where(['id' => '[0-9]', 'name' => '[A-Za-z]+']);
    // 路由别名
    Route::get('user/user_center',['as'=>'center',function(){
        return 'user_center';
        return route('center'); // 会得到当前完整的路由,别名的作用出来了
    }]);
    */
    
    
    //路由群组
    // prefix 路有前缀,访问时需要域名+ 前缀后访问  例如:127.0.0.1/api/id/1
    Route::group(['prefix' => 'api'], function () {
    
        Route::get('id/{id}', function ($id) {
            return 'id-' . $id;
        });
    
        Route::get('user/user_center', ['as' => 'center', function () {
            return 'user_center';
            return route('center'); // 会得到当前完整的路由,别名的作用出来了
        }]);
    });
    
    // 路由中输出视图
    Route::get('view', function () {
        return view('welcome');
    });
    
    // 关联到控制器的路由
    Route::get('cate/all', 'CateController@getCate');
    Route::get('cate/all', ['uses' => 'CateController@getCate']);
    // 别名
    Route::get('cate/all', [
        'uses' => 'CateController@getCate',
        'as' => 'cate'
    ]);
    
Copyright © mos360.cn By Moons Soft Studio 百度统计