Laravel基础知识之路由

1.路由简介:将用户请求转发给相应程序进行处理,建立URL和程序之间的映射。

2.设置路由的文件,根目录下\routes\web.php

3.基础路由:请求方式有get,post,put,patch,delete。单请求路由:

当访问localhost/hellow时,返回的是Hellow World字符串。

//地址栏输入localhost/hellow,将会执行后面的函数输出"Hellow World"
<?php
Route::get('hellow',function(){
  return "Hellow World";
});                            
?>

4.多请求路由:当发出get或post请求时,都可以执行。

//以get或者post都可以访问
<?php
Route::match(['get','post'],function(){
  return "match";
});
?>

5.any路由,不管是什么形式的请求都可以执行。

<?php
Route::any('hellow',function(){
  return "Hellow World";
});

6.路由参数,{id}是参数,将会传递到后面要执行的函数中

 

//访问localhost/name/5将会输出name-5
<?php
Route::get('name/{id}',function($id){
  return "name-".$id;
});

7.路由别名:

//访问localhost/user/center将会返回localhost/user/center
<?php
Route::get('user/center',['as' => 'center',function(){
  return route('center');
}]);

8.路由群组:

//将多个路由规则放在群组里
<?php
Route::group(['prefix' => 'member'],function(){
  Route::get('hellow',function(){
    return "Hellow World";
  });

  Route::get('Hai',function(){
    return "Hellow World";
  });
});  

  

扫描二维码关注公众号,回复: 904586 查看本文章

 

  

猜你喜欢

转载自www.cnblogs.com/xichang/p/9053191.html