编程学习网 > 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 href="https://fonts.googleapis.com/css?family=Raleway:100,600" 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高级编程视频教程免费获取