路由、控制器笔记

1 基础路由写法

Route::get('/index',function (){
    return 'hello word';
});
//http://127.0.0.1:8000/index

 2、match 接收指定的提交方式

Route::match(['get','post'],'match',function (){
    return 'match';
});

3 、路由中传递参数

Route::get('index/{id}',function ($id){
    return 'index'.$id;
});
//http://127.0.0.1:8000/index/abc123 返回indexabc123

 4、控制其中传参

Controllers/TestController.php

public function read($id){

        return 'id:'.$id;

    }
Route::get('/test/read/{id}','TestController@read');
// http://127.0.0.1:8000/test/read/1 返回1
Route::get('/test/read/{id}','TestController@read')->where('id','[0-9]+');  //限定传递参数   使用正则表达式
//  http://127.0.0.1:8000/test/read/1 返回1 只能是数字

 也可以是数组

Route::get('/test/read/{id}','TestController@read')->where(['id'=>'[0-9]+']);

 路由全局限定

app\Providers\RouteServiceProvider.php文件中

 public function boot()
    {
        Route::pattern('id','[0-9]+');
        //

        parent::boot();
    }

限定解除  

where(['id'=>'.*']);

5、路由重定向

Route::redirect('index','test',301);

6 、视图路由

Route::view('news','news');

第一个参数为url  第二个参数为 模板名称

第二中方法

Route::get('shop',function (){
    return view('shop');
});

 7、获取url

public function url(){
        $url=Route('shop');
        return $url;
    }
也可以传递参数
$url=Route('shop',['id'=>'10']);

 8  路由分组

Route::group(['prefix'=>'admin'],function (){
    Route::get('index','AdminController/index');

});
原文地址:https://www.cnblogs.com/linzenews/p/12727275.html