构建基本路由只需要一个 URI 与一个 闭包 路由文件地址 routes文件夹 包含 api.php channels.php console.php web.php web.php 1 2 3 4 5 6 7 8 9 10 11 12 Route::get('/' , function () { return view('welcome' ); }); 方式 Route::get($uri , $callback ); Route::post($uri , $callback ); Route::put($uri , $callback ); Route::patch($uri , $callback ); Route::match(['请求方式' ]$uri , $callback ); Route::delete($uri , $callback ); Route::options($uri , $callback ); Route::any($uri , $callback );
根目录打开命令行 1 php artisan make:controller MyController
系统自动生成 app/Http/Controllers/MyController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 //建立方法 public function get (){ return "get" ; } public function post (){ return "post" ; } public function put (){ return "put" ; } public function patch (){ return "patch" ; } public function option (){ return "option" ; } public function delete (){ return "delete" ; } public function match (){ return "match['post','get']" ; } public function any (){ return "any ['all']" ; }
1 2 3 4 5 6 7 8 9 //定义路由 web.php Route::get('/user/get' ,'MyController@get' ); Route::post('/user/post' ,'MyController@post' ); Route::put('/user/put' ,'MyController@put' ); Route::patch('/user/patch' ,'MyController@patch' ); Route::options('/user/option' ,'MyController@option' ); Route::delete('/user/delete' ,'MyController@delete' ); Route::match(['get' ,'post' ],'/user/match' ,'MyController@match' ); Route::any('/user/any' ,'MyController@any' );
因为有CSRF保护机制练习前需要关闭保护 https://learnku.com/docs/laravel/7.x/csrf/7460
中间件下的 app/Http/Middleware/VerifyCsrfToken.php
1 2 3 4 5 protected $except = [ //配置servername 'http://{servername}/user/*' , ]; }
使用 PostMan请求
redirect 路由重定向 1 2 3 Route::get('log/here' ,'LogController@here' ); Route::get('log/there' ,'LogController@there' ); Route::redirect('log/here' ,'/log/there' );
view路由视图 1 Route::view('/view' ,'view' ,['name' =>'jon' ]);
视图取值 {{ name }}路由参数 正则验证 {name?}可为空,可设置默认值 1 2 3 Route::get('/get/{id}/{name?}' ,function ($id ,$name ='tom' ){ return "UserID:" .$id ." Username:" .$name ; });
设置全局正则 匹配 参数 id 必须整数 ‘[0-9]+’ 的正则 打开文件 app\Providers\RouteServiceProvider.php 1 2 3 4 5 public function boot (){ //加入这个限制所有变量参数id 的验证 Route::pattern('id' ,'[0-9]+' ); parent::boot(); }
行内正则验证 aid 1 2 3 Route::get('/bl/{aid}' ,function ($aid ){ return "UserID:" .$aid ; })->where ('aid' ,'[0-9]+' );
隐式绑定官方例子 1 2 3 Route::get('/user/{user}' ,function (App\User $user ){ return $user ->name; });
新建一个表测试一下 1 2 3 4 5 6 CREATE TABLE `contents` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(222) DEFAULT NULL, `content` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
创建model 1 php artisan make:model Content
打开model 设置绑定的条件 1 2 3 4 public function getRouteKeyName (){ //这个地方可以定义任意contents表的段 return 'id' ; }
添加路由 ,有数据返回对应值,无数据 返回404 1 2 3 Route::get('/content/{content}' ,function (App\Content $content ){ return $content ->title; });
有隐式就有显示 显示绑定 1 2 3 4 //这里面定义的 $user 对应 Route::get('/user/{user}' ,function ($user ){ dd($user ->name); });
RouteServiceProvider.php 通过这里给{user}绑定 \App\User 1 2 3 4 5 6 public function boot() { parent::boot(); //顶一个 user 对应 \app\User Route::model('user' , \App\User::class); }