联系QQ 284710375
首页 > 技术分享 > Laravel
收藏

Laravel5.2路由详解2020-06-09 10:24:40

大潇博客 原创文章,转载请标明出处

Laravel路由配置多姿多彩,访问Laravel任意部分,都需要先配置路由,laravel5.2路由文件位置:/app/Http/routes.php,支持get、post、put、delete、patch、options六种请求方式


打开routes.php文件可以看到:

Route::get('/', function () {

return view('welcome');

});

这是默认请求打开的模板,文件为/resources/views/welcome.blade.php

由此可见,模板文件位置“/resources/views/”


laravel5.2仅支持的六种路由请求类型:get、post、put、delete、patch、options

Route::get('/cc', function() {

echo 'get';

});

Route::post('/cc', function() {

echo 'post';

});

Route::put('/cc', function() {

echo 'put';

});

Route::delete('/cc', function() {

echo 'delete';

});

Route::patch('/cc', function() {

echo 'patch';

});

Route::options('/cc', function() {

echo 'options';

});



路由请求方式合并,使用路由合并, 可以一次设置多种请求方法

Route::match(['get', 'post', 'patch'], '/test', function() {

echo 'match => get && post';

});



任意方式请求

Route::any('/foo', function() {

echo 'any';

});



路由带参数

#如:http://localhost/laravel52/public/index.php/user/10,这时user后面的参数必须要带,否则会报没有定义方法的错误

Route::get('/user/{id}', function($id) {

return 'user'.$id;

});

#地址栏参数和function中的变量名,可以不一致,function中的变量名和输出的变量名需要一致,如下,等于把{id}赋值给$i

Route::get('/users/{id}', function($i) {

return 'user'.$i;

});

#根据上面的介绍,扩展多个参数

Route::get('/posts/{post}/comments/{comment}', function($postId, $commentId){

return 'post:'.$postId.',comment:'.$commentId;

});



路由中的参数可选

#“?”表示传参可选,后面的闭包函数中的参数,也要做处理,可定义null,也可定义实际字符串

Route::get('/abc/{id?}', function($id=null) {

return 'abc'.$id;

});

#多个可选参数

Route::get('/ppp/{p?}/bbb/{b?}', function($p='p', $b='b') {

return $p . '---' . $b;

});

#注意:这种情况只有最后一个可选,如果两个都不填,如:http://localhost/laravel52/public/index.php/ppp/bbb/,则会报错(laravel不确定第二个是参数,还是用来接收参数的),建议把可选的放在最后位置



做验证,限制类型,防止攻击,防止sql注入等,在每段Route最后加入where条件,条件格式和正则表达式中的一些规则相同

#参数id为0到9的数字,个数0-n

Route::get('/user/{id?}', function($id='666'){

return 'user'.$id;

})->where("id","[0-9]*"); 

#参数str只能是a-z的字母,1-n个

Route::get('/str/{str?}', function($str = 'abc'){

return 'string:'.$str;

})->where("str", '[a-zA-Z]+'); 

#多个变量的匹配方式

Route::get('/cs/{id}/{name}', function($id, $name){

return "id:".$id." - name:".$name;

})->where(["id"=>"[0-9]+", "name"=>"[a-z]+"]); 



路由定义控制器,下面是Laravel调用控制器的两种路由定义方法

#写法一,直接在引号中写入控制器

Route::get('/index', 'IndexController@index');

#写法二,定义一个数组,把控制器路由赋值给“uses”,多加了Admin是因为多了一层文件夹,把控制器做了分类,可用来区分前台和后台代码,注意用反斜线“\”

Route::get('/admin', ['uses' => 'Admin\IndexController@index']);


路由命名,通过“as”设置别称,获取当前url地址

Route::get('/test', ['as' => 'profile', function () {

echo route('profile'); #route为laravel函数,其中参数为刚才设置的路由别称,用来输出当前地址,例如http://l.com/index.php/test

return "路由命名";

}]);

#下面两种设置路由别称的方法,结果一样,推荐第二种

#第一种

Route::get('/ceshi', [

'as' => 'profile',

'uses' => "Ceshi\CeshiController@index",

]);

#第二种

Route::get('/ceshi', 'Ceshi\CeshiController@index')->name('profile');



路由分组

#我们在开发时,可能需要把不同用途的控制器,分别放到不同的文件夹中,例如后台相关的控制器放到“admin”文件夹中,url为此也会有相同的参数“admin”,常规写法是这样

Route::get('/admin/login', 'Admin\IndexController@login');

Route::get('/admin/home', 'Admin\IndexController@home');

#分组后的写法

Route::group(['prefix' => 'admin'], function () {

Route::get('login', 'Admin\IndexController@login');

Route::get('home', 'Admin\IndexController@home');

});

#我们还可以在分组中定义namespace,表示当前组的控制器所在文件夹,使代码简洁明了

Route::group(['prefix'=>'admin', 'namespace'=>'Admin'], function() {

Route::get('login', 'IndexController@login');

Route::get('home', 'IndexController@home');

#配置路由资源(本文不讲解)

Route::resource('article', 'ArticleController');

});


打赏

阅读排行

大家都在搜

博客维护不易,感谢你的肯定
扫码打赏,建议金额1-10元
  • 15601023311