laravel框架,必須先設置路由,才能夠訪問內部的控制器部分。php
路由文件:routes/web.php.html
基本路由laravel
Route::get('/user', 'UserController@index');
可用的路由方法web
Route::get($uri, $callback); Route::post($uri, $callback); Route::put($uri, $callback); Route::patch($uri, $callback); Route::delete($uri, $callback); Route::options($uri, $callback);
重定向路由api
// 重定向 訪問http://claravel57.com/da 會跳轉到http://claravel57.com/there Route::redirect('da','there',301); Route::redirect('db','http://www.baidu.com',301);
視圖路由composer
Route::view('/welcome', 'welcome'); Route::view('/welcome', 'welcome', ['name' => 'Taylor']);
路由參數框架
Route::get('user/{id}', function ($id) { return 'User '.$id; });
Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) {
//
});
路由傳參數post
// 必選傳參數 Route::get('user/{id}', function ($id) { return 'User '.$id; }); // 可選傳參數,由問號決定 Route::get('user/{id?}', function ($id) { return 'User '.$id; });
還有不少的路由方式,能夠查閱官方手冊spa
路由在實際項目中應用code
場景一:單模塊路由(Index)
Route::get('/user','UserController@index');
場景二:多個模塊路由 (Admin,Index,Api),採用羣組路由
/* * api路由羣組 * 訪問格式 http://localhost/xxx/ * @param prefix 路由別名 * @param namespace 別名對應的命名空間(Admin,Index,Api) * */ // 路由羣組 訪問:http://localhost/admin/goods Route::group(['prefix' => 'admin', 'namespace' => 'Admin'] ,function () { //Admin模塊 //Route::get('/控制名/方法','IndexController@index'); Route::get('/goods/','IndexController@index'); }); // 路由羣組 訪問:http://localhost/index/index Route::group(['prefix' => 'index', 'namespace' => 'Index'] ,function () { //Index模塊 Route::get('/index','IndexController@index'); }); // Api模塊路由羣組 訪問:http://localhost/api/info Route::group(['prefix' => 'api', 'namespace' => 'Api'] ,function () { //api模塊 Route::get('/info/','IndexController@index'); });
若是不使用羣組路由,路由格式
Route::get('/admin/goods/index','Admin\IndexController@index'); Route::get('/Index/index/index','Admin\IndexController@index'); Route::get('/Api/info/index','Admin\IndexController@index');
使用羣組路由的好處:每一個模塊下的路由更加美觀,敲打的代碼更少;多模塊部署推薦使用羣組路由
場景三:路由與控制器之間的傳參
單個參數:Route::get('user/{id}', function ($id) { return 'User '.$id; });
多個參數:Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) { // }); // 此方法有弊端(很是多的參數時候,路由會寫的很長),不推薦使用,能夠使用一下傳參數
路由:Route::get('user/', function ($id) { return 'User '.$id; });
// 頁面Get傳參數 http://xx.com?id=1&name=MR.zhou&age=18&sex=1 // 路由web.php Route::get('/Api/info/index','Admin\IndexController@index'); // 控制器 public function index(){ $param = Request()->input(); $name = $param['name'] }
場景四:路由引用中間件(登錄檢測)
Route::get('/index','IndexController@index')->middleware('goods');
場景五:路由回滾
// 路由回滾 定義一些不存在的頁面 Route::fallback(function () { return '404'; });
容易報錯
除了默認路由,其餘路由模式沒法訪問
server { listen 80; server_name l58.com; root "D:/phpStudy/PHPTutorial/WWW/composer/l58/public"; location / { index index.html index.htm index.php; #autoindex on; try_files $uri $uri/ /index.php?$query_string; // 路由訪問失效,加上此處 } location ~ \.php(.*)$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^((?U).+\.php)(/?.+)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; include fastcgi_params; } }
提示類不存在:Class App\Http\Controllers\Index/IndexController does not exist
Route::get('/Api/info/index','Admin/IndexController@index'); // 錯誤 Route::get('/Api/info/index','Admin\IndexController@index'); // 正確