編程學習網 > PHP技術 > laravel > laravel路由原理分析簡介
2021
08-05

laravel路由原理分析簡介

在學習一個框架的時候,搞清楚路由是重中之重,不然在瀏覽器怎么訪問開發的頁面都不知道


laravel里路由的關聯:


這里介紹些學習到的路由!


路由重定向


//重定向

Route::redirect('/here', '/there', 301);


訪問:http://wx.dmtnewton/here

結果:http://wx.dmtnewton/there (頁面自動跳轉)


路由視圖


#file path: Laravel/routes/web.php

//頁面展示:Laravel/resources/views/welcome.blade.php 模板的內容

//將路由參數 name 賦值到 模板變量 name

Route::get('/user/{name}', function ($name = 'newton') {

   return view('welcome', ['name' => $name]);

});



<!-- file path: Laravel/resources/views/welcome.blade.php -->

<!doctype html>

<html lang="{{ app()->getLocale() }}">

   <head>

       <meta charset="utf-8">

       <meta http-equiv="X-UA-Compatible" content="IE=edge">

       <meta name="viewport" content="width=device-width, initial-scale=1">



       <title>Laravel</title>



       <!-- Fonts -->

       <link  rel="stylesheet" type="text/css">

   </head>

   <body>

       <h3>{{ $name }}</h3> <!-- 這里就是PHP中示例的變量 -->

       <div class="flex-center position-ref full-height">

           @if (Route::has('login'))

               <div class="top-right links">

                   @auth

                       <a href="{{ url('/home') }}">Home</a>

                   @else

                       <a href="{{ route('login') }}">Login</a>

                       <a href="{{ route('register') }}">Register</a>

                   @endauth

               </div>

           @endif



           <div class="content">

               <div class="title m-b-md">

                   Laravel

               </div>



           </div>

       </div>

   </body>

</html>


訪問:http://wx.dmtnewton/user/welcome

結果:


路由參數-正則約束


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

   return "ID: {$id}, TYPE: {$type}";

})->where(['id' => '\d+', 'type' => '[\w]+']);


訪問:http://wx.dmtnewton.com/user/123/_/English

結果:ID: 123, TYPE: English


訪問:http://wx.dmtnewton.com/user/ddd/_/English

結果:(Laravel默認提示)

Sorry, the page you are looking for could not be found.


命名路由


//命名路由

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

   return 'URL: ' . route('profile');

})->name('profile');


訪問:http://wx.dmtnewton.com/user/profile

結果:URL: http://wx.dmtnewton.com/user/profile


//命名傳參路由

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

   $url = route('prof', ['id' => $id]);

   return $url;

})->name('prof');


訪問:http://wx.dmtnewton.com/user/123/prof

結果:http://wx.dmtnewton.com/user/123/prof


路由前綴


Route::prefix('admin')->group(function(){

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

       return 'The Url is belong of Admin. ';

   });

});


訪問:http://wx.dmtnewton.com/admin/menu

結果:The Url is belong of Admin.


子域名路由


Route::domain('{account}.dmtnewton.com')->group(function(){

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

       return "There is {$account} page of user {$id}";

   });

});


訪問:http://wx.dmtnewton.com/user_/2

結果:There is wx page of user 2


重定向到命名路由


//重定向到命名的路由

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

   //生成重定向

   return redirect()->route('profile');

});


訪問:http://wx.dmtnewton.com/redirect

結果:http://wx.dmtnewton.com/user/profile


默認參數


//頁面訪問時,如果沒有參數,添加默認

Route::get('/{age?}', function ($age = 18) {

   return "Age: {$age}";

});


訪問:http://wx.dmtnewton.com/

結果:Age: 18

以上就是“laravel路由原理分析簡介”的詳細內容,想要了解更多laravel教程歡迎關注編程學習網

掃碼二維碼 獲取免費視頻學習資料

Python編程學習

查 看2022高級編程視頻教程免費獲取