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 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}}
使用传入的参数