基本路由
| |
| Route::get('hello', function () { |
| return 'Hello, Laravel'; |
| }); |
| |
| |
| Route::get($uri, $callback); |
| Route::post($uri, $callback); |
| Route::put($uri, $callback); |
| Route::patch($uri, $callback); |
| Route::delete($uri, $callback); |
| Route::options($uri, $callback); |
| |
| |
| Route::match(['get', 'post'], '/', function () { |
| |
| }); |
| |
| |
| Route::any('foo', function () { |
| |
| }); |
路由参数
- 使用花括号包裹
- 路由参数不能包含 - 字符, 需要的话可以使用 _ 替代
| |
| Route::get('user/{id}', function ($id) { |
| return 'User '.$id; |
| }); |
| |
| |
| Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) { |
| |
| }); |
| |
| |
| Route::get('user/{name?}', function ($name = null) { |
| return $name; |
| }); |
| Route::get('user/{name?}', function ($name = 'John') { |
| return $name; |
| }); |
| |
| |
| Route::get('user/{name}', function ($name) { |
| |
| })->where('name', '[A-Za-z]+'); |
| |
| Route::get('user/{id}', function ($id) { |
| |
| })->where('id', '[0-9]+'); |
| |
| Route::get('user/{id}/{name}', function ($id, $name) { |
| |
| })->where(['id' => '[0-9]+', 'name' => '[a-z]+']); |
命名路由
| |
| Route::get('user/profile', function () { |
| |
| })->name('profile'); |
| |
| |
| Route::get('user/profile', 'UserController@showProfile')->name('profile'); |
| |
| |
| $url = route('profile'); |
| return redirect()->route('profile'); |
| |
| |
| Route::get('user/{id}/profile', function ($id) { |
| |
| })->name('profile'); |
| $url = route('profile', ['id' => 1]); |
路由群组
中间件
| Route::group(['middleware' => 'auth'], function () { |
| Route::get('/', function () { |
| |
| }); |
| Route::get('user/profile', function () { |
| |
| }); |
| }); |
命名空间
| Route::group(['namespace' => 'Admin'], function(){ |
| |
| }); |
子域名路由
| Route::group(['domain' => '{account}.myapp.com'], function () { |
| Route::get('user/{id}', function ($account, $id) { |
| |
| }); |
| }); |
路由前缀
| Route::group(['prefix' => 'admin'], function () { |
| Route::get('users', function () { |
| |
| }); |
| }); |
表单方法伪造
| <form action="/foo/bar" method="POST"> |
| <input type="hidden" name="_method" value="PUT"> |
| <input type="hidden" name="_token" value="{{ csrf_token() }}"> |
| </form> |
或使用辅助函数 method_field
:
| {{ method_field('PUT') }} |
访问当前路由
| $route = Route::current(); |
| $name = Route::currentRouteName(); |
| $action = Route::currentRouteAction(); |
路由缓存
| |
| php artisan route:cache |
| |
| php artisan route:clear |
路由模型绑定
隐式绑定
| |
| Route::get('api/users/{user}', function (App\User $user) { |
| return $user->email; |
| }); |
| |
| |
| |
| |
| |
| |
| |
| public function getRouteKeyName() |
| { |
| return 'slug'; |
| } |
显式绑定
要注册显式绑定, 需要使用路由的 model
方法来为给定参数指定绑定类. 应该在 RouteServiceProvider
类的 boot
方法中定义模型绑定:
| public function boot() |
| { |
| parent::boot(); |
| Route::model('user', App\User::class); |
| } |
定义一个包含 {user}
参数的路由:
| $router->get('profile/{user}', function(App\User $user) { |
| |
| }); |
如果请求 URL
是 profile/1
, 就会注入一个用户 ID
为 1
的 User
实例, 如果匹配的模型实例在数据库不存在, 会自动生成并返回 HTTP 404
响应.
自定义解析逻辑
如果你想要使用自定义的解析逻辑, 需要使用 Route::bind
方法, 传递到 bind
方法的闭包会获取到 URI
请求参数中的值, 并且返回你想要在该路由中注入的类实例:
| public function boot() |
| { |
| parent::boot(); |
| Route::bind('user', function($value) { |
| return App\User::where('name', $value)->first(); |
| }); |
| } |
文章来源于本人博客,发布于 2018-06-02,原文链接:https://imlht.com/archives/155/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?