首頁  >  文章  >  php框架  >  一篇文章學會Laravel框架所有基礎路由知識

一篇文章學會Laravel框架所有基礎路由知識

Mini
Mini原創
2020-05-21 15:51:21110瀏覽

本節內容主要講解Laravel框架的路由詳解,主要講解路由的定義,參數,規則,存取控制器等操作。

一、常用路由動作

#
Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);

常用路由方式

Route::post('/', function () {    
    return view('welcome');
});
Route::get('/', function () {    
    return view('welcome');
});
Route::delete('/', function () {    
    return view('welcome');
});


#或直接請求控制器

Route::post('/',  'WelcomeController@index');
Route::get('/',  'WelcomeController@index');
Route::delete('/',  'WelcomeController@index');

任何請求方式匹配

Route::any(['get', 'post'], '/', function () {
    return view('welcome');
});
Route::any(['get', 'post'], '/', 'WelcomeController@index');

白名單數組匹配match組合

Route::match(['get', 'post'], '/', function () {
    return view('welcome');
});
Route::match(['get', 'post'],'/', 'WelcomeController@index');

重定向路由

Route::redirect('/here',  '/404',  404);

二、參數路由

Route::get('user/{id?}', function ($id = 1) {    
    return "用户ID: " . $id;
});


正規方式的參數路由

Route::get('page/{id}', function ($id) {    
    return '页面ID: ' . $id;
})->where('id', '[0-9]+');
Route::get('page/{name}', function ($name) {    
    return '页面名称: ' . $name;
})->where('name', '[A-Za-z]+');
Route::get('page/{id}/{slug}', function ($id, $slug) {    
    return $id . ':' . $slug;
})->where(['id' => '[0-9]+', 'slug' => '[A-Za-z]+']);


##三、路由別名(以user.profile進行存取)

Route::get('user/{id?}', function ($id = 1) {    
    return "用户ID: " . $id;
})->name('user.profile');

// 存取方式:

<a href="{{ route(&#39;user.profile&#39;, [&#39;id&#39; => 100]) }}">
四、路由分組

Route::group([], function () {     
    Route::get(&#39;hello&#39;, function () {         
        return &#39;Hello&#39;;     
    });     
    Route::get(&#39;world&#39;, function () {         
        return &#39;World&#39;;     
    });
});
五、路由分組、中間件(2中方式-一般使用第二種)

Route::middleware(&#39;auth:api&#39;)->group(function () {    
    Route::get(&#39;dashboard&#39;, function () {        
        return view(&#39;dashboard&#39;);    
    });    
    Route::get(&#39;account&#39;, function () {        
        return view(&#39;account&#39;);    
    });
});

Route::group([&#39;middleware&#39; => &#39;auth:api&#39;], function () {     
    Route::get(&#39;dashboard&#39;, function () {         
        return view(&#39;dashboard&#39;);     
    });     
    Route::get(&#39;account&#39;, function () {         
        return view(&#39;account&#39;);     
    });
});
#六、路由路徑前綴

Route::prefix(&#39;api&#39;)->group(function () {    
    Route::get(&#39;/&#39;, function () {        
    // 处理 /api 路由    
    })->name(&#39;api.index&#39;);   
     
    Route::get(&#39;users&#39;, function () {        
    // 处理 /api/users 路由    
    })->name(&#39;api.users&#39;);
});
七、路由子網域

Route::domain(&#39;{account}.blog.test&#39;)->group(function (){    
    Route::get(&#39;/&#39;, function ($account) {        
        //TODO    
    });    

    Route::get(&#39;user/{id}&#39;, function ($account, $id) {        
        //TODO    
    });
});
#八、路由命名空間

Route::namespace(&#39;Admin&#39;)->group(function() {     
    // App\Http\Controllers\Admin\AdminController     
    Route::get(&#39;/admin&#39;, &#39;AdminController@index&#39;);
});

路由命名空間、前綴、分組、參數、別名組合

// 路由命名路徑前綴

#

Route::name(&#39;user.&#39;)->prefix(&#39;user&#39;)->group(function () {    
    Route::get(&#39;{id?}&#39;, function ($id = 1) {        
        // 处理 /user/{id} 路由,路由命名为 user.show        
        return route(&#39;user.show&#39;);    
    })->name(&#39;show&#39;);  
      
    Route::get(&#39;posts&#39;, function () {        
        // 处理 /user/posts 路由,路由命名为 user.posts    
    })->name(&#39;posts&#39;);
});




##### #########九、存取目前路由#########
$route  = Route::current();
$name   = Route::currentRouteName();
$action = Route::currentRouteAction();
##########十、清除路由快取########
php artisan route:cache
## #######十一、刪除路由快取############
php artisan route:clear
################################ ######總結:###############透過以上的學習總結,我們已經學會了Laravel的路由定義、訪問方式,而且路由非常的方便,對於新手來說,學習起來非常的方便,也非常容易學會。 ###

以上是一篇文章學會Laravel框架所有基礎路由知識的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn