laravel中的路由

首先,laravle中的路由基本上都是在routes文件夹中的web.php文件中创建的,

1.通常使用的是定义get方法和post方法创建的路由,

Route::get('haha',function(){
    return 'hahaha';
})

这个路由使用了定义了get方法,同时定义了一个闭包函数,同时get也可改为post方法

2.同样也可以使用any方法来满足所有的请求方法,

Route::any('hhh','User@hello');
通常http请求有get,post,put,patch,delete,options

3.加入要在路由中定义参数, { } 花括号中代表可填的参数,where条件中代表对参数的正则验证

Route::get('user/{name?}/{id?}', function ($name='',$id='') {
    return $name.'='.$id;
})->where([
    'name'=>'[a-zA-Z]+',
    'id'=>'[0-9]+'
]);

4.重定向路由

重定向路由
Route::get('haha',function(){ return 'hahaha'; })->name('h'); //name定义了这个路由的'小名' Route::get('abc',function(){ return redirect()->route('h'); //进行跳转 ,同样会输出结果'hahaha' });

 

 
路由器跳转
Route::get('testa',function(){ return redirect('aaa'); //路由器进行跳转,会输出'aaa'的结果 });

 

外部网站进行跳转

Route::get('testb',function(){ return redirect('http://www.baidu.com'); });

 

 

5.视图路由,如果你的路由只需要返回一个视图,可以使用 Route::view 方法,view 方法有三个参数,其中前两个是必填参数,分别是 URL 和视图名称。

第三个参数选填,可以传入一个数组,数组中的数据会被传递给视图。

 

Route::view('net','layout.test',[
    'records'=>7,
    'a'=>3
]);

 

posted on 2019-09-05 13:43  杜梦龙  阅读(183)  评论(0)    收藏  举报

导航