lavarel从零开始-第三弹(路由,控制器,视图)
Posted zfyan-1992
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lavarel从零开始-第三弹(路由,控制器,视图)相关的知识,希望对你有一定的参考价值。
routes/web.php写路由
基本路由:
Route::get(‘/‘, function () {
return view(‘welcome‘);
});
Route::get(‘two‘,function(){
return view(‘two‘);
});
Route::get(‘three‘,function(){
return view(‘three‘);
});
Route::get(‘four‘,function(){
return ‘four‘;
});
多请求路由:
Route::match([‘get‘,‘post‘],‘multy1‘,function(){
return ‘get match‘;
});
Route::any(‘multy2‘,function(){
return ‘get any‘;
});
路由传参:
Route::get(‘user/{id}‘,function($id){
return ‘user id is ‘.$id;
});
路由别名:
Route::get(‘user2/center‘,[‘as‘=>‘center‘,function(){
return route(‘center‘);
}]);
路由群组:
Route::group([‘prefix‘=>‘member‘],function(){
Route::get(‘user/{id}/{name?}‘,function($id,$name=‘sean‘){
return ‘user id is ‘.$id.‘ user name is ‘.$name;
})->where([‘id‘=>‘[0-9]+‘,‘name‘=>‘[A-Za-z]+‘]);
Route::get(‘user2/center‘,[‘as‘=>‘center‘,function(){
return route(‘center‘);
}]);
});
控制器:
控制器写在http/controllers。新建文件MemberController.php:
<?php
namespace AppHttpControllers;
class MemberController extends Controller
{
public function info()
{
return ‘member-info‘;
}
}
?>
路由和控制器相关联:
方法一:Route::get(‘member/info‘,‘[email protected]‘);
方法二:Route::get(‘member/info‘,[‘uses‘=>‘[email protected]‘]);
控制器传参:
Route::get(‘member/info/{id}‘,‘[email protected]‘)->where(‘id‘,‘[1-9]+‘);
视图:
控制器中:
<?php
namespace AppHttpControllers;
class MemberController extends Controller
{
public function info($id)
{
//return ‘member-info‘.$id;
//return route(‘memberinfo‘);
return view(‘four‘,[
‘name‘ => ‘zfy‘,
‘age‘ => 14
]);
}
}
?>
视图中:
asdf
{{$name}}{{$age}}
使用传入的参数
以上是关于lavarel从零开始-第三弹(路由,控制器,视图)的主要内容,如果未能解决你的问题,请参考以下文章
WEB接口测试之Jmeter接口测试自动化 第三弹(推倒重铸)