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:
<?phpnamespace App\Http\Controllers;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]+‘); 视图:控制器中:
<?phpnamespace 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}}使用传入的参数
原文地址:https://www.cnblogs.com/zfyan-1992/p/9520319.html
时间: 2024-11-08 09:22:32