首页 > 其他 > 详细

lavarel从零开始-第三弹(路由,控制器,视图)

时间:2018-08-22 21:39:47      阅读:191      评论:0      收藏:0      [点我收藏+]

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 App\Http\Controllers;
class MemberController extends Controller
{
public function info()
{
return ‘member-info‘;
}
}
?>

路由和控制器相关联:
方法一:Route::get(‘member/info‘,‘MemberController@info‘);
方法二:Route::get(‘member/info‘,[‘uses‘=>‘MemberController@info‘]);
控制器传参:
Route::get(‘member/info/{id}‘,‘MemberController@info‘)->where(‘id‘,‘[1-9]+‘);

视图:
控制器中:
<?php
namespace App\Http\Controllers;
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从零开始-第三弹(路由,控制器,视图)

原文:https://www.cnblogs.com/zfyan-1992/p/9520319.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!