Blogger Information
Blog 4
fans 0
comment 0
visits 7873
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
laravel 初识路由
colonel的博客
Original
791 people have browsed it

路由文件名:routes.php   路径 app/Http/routes.php(此处以laravel 5.2 版本为例)

<?php

Route::get('/', function () {
   return view('welcome'); //  访问  127.0.0.1/你的框架文件名/public/hello”  路由显示视图‘welcome’
});

Route::get('hello',function(){
   return 'hello world';  //   访问  127.0.0.1/你的框架文件名/public/hello”          路由输出 ‘hello world’
});

//基础路由
Route::match(['get','post'],'nihao',function(){
   return '你好!'; // 访问“127.0.0.1/你的框架文件名/public/nihao”页面返回显示‘你好’
});

路由参数

//浏览器路径  127.0.0.1/你的框架文件夹名/public/user/name( name 就是你的参数)
Route::get('user/{name?}',function($name = '瓜皮'){
   return 'user-name-'.$name;
})->where('name','[A-Za-z]+');

//限制规则(正则):只有当name只由字母组成的时候此路由生效

//浏览器路径127.0.0.1/你的框架文件夹名/public/user/你的id
Route::get('user/{id}',function($id=null){
   return 'user-id-'.$id;
});

//浏览器路径127.0.0.1/你的框架文件夹名/public/user/你的id/你的name

Route::get('user/{id}/{name?}',function($id,$name = null){
   return 'user-id:'.$id.'&nbsp;user-name:'.$name;
})->where(['id' => '[0-9]+','name' => '[A-Za-z]+']);

//限制规则(正则):只有当id为数字、name只由字母组成的时候此路由生效



//路由别名

浏览器路径127.0.0.1/你的框架文件夹名/public/user/center

/*Route::get('user/center',['as'=>'center',function(){
   return route('center');  //浏览器返回别名为 ‘center’ 的路由路径
}]);

//路由群组,路由群组内部的路由均被添加一个上级(prefix 的值)


浏览器路径127.0.0.1/你的框架文件夹名/public/vip/user/center(left)

Route::group(['prefix' => 'vip'],function(){
    Route::get('user/center',['as'=>'center',function(){
        return route('center');
    }]);
    Route::get('user/left',['as'=>'left',function(){
        return route('left');
    }]);
});

路由输出试图

Route::get('view',function(){
   return view('welcome');//视图文件路径/resources/view/
});

路由连接控制器

Route::get('index','IndexController@index');
Route::get('index',['uses' =>'IndexController@index']);
//IndexController为控制器名  index 为方法名

Route::get('index',[
    'uses' => 'IndexController@index',
    'as'   => 'Index'
])
Route::get('index/{id}',['uses' =>'IndexController@index'])->where('id','[0-9]+');

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post