laravel 5.5 -- router
来源:互联网 发布:淘宝直播招聘主播吗 编辑:程序博客网 时间:2024/05/23 00:41
路由
resource
* GET /testindex() // Display a listing of the resource.* GET /test/createcreate() // Show the form for creating a new resource.* POST /teststore() // Store a newly created resource in storage.* GET /test/{id}show($id) // Display the specified resource.* GET /test/{id}/editedit($id) // Show the form for editing the specified resource.* PUT /test/{id}update($id) // Update the specified resource in storage.* DELETE /test/{id} destroy($id) // Remove the specified resource from storage.
隐式绑定
Route::get('api/users/{user}', function (App\User $user) { return $user->email; // 传人的 id});# 自定义键名 在模型中修改public function getRouteKeyName(){ return 'slug';}
显式绑定
# RouteServiceProviderpublic function boot(){ parent::boot(); Route::model('user', App\User::class);}Route::get('profile/{user}', function ($user) { //});
自定义解析逻辑
public function boot(){ parent::boot(); Route::bind('user', function ($value) { return App\User::where('name', $value)->first() ?? abort(404); });}
基本路由
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::redirect('/here', '/there', 301);# 只需要返回一个视图Route::view('/welcome', 'welcome', ['name' => 'Taylor']);
路由参数
Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) { //});Route::get('user/{name?}', function ($name = 'John') { return $name;});
正则表达式约束
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]+']);
全局约束
public function boot(){ Route::pattern('id', '[0-9]+'); parent::boot();}
命名路由
Route::get('user/profile', function () { //})->name('profile');// 使用$url = route('profile');// 生成重定向...return redirect()->route('profile');
// 检查当前路由public function handle($request, Closure $next){ if ($request->route()->named('profile')) { // } return $next($request);}
中间件
Route::middleware(['first', 'second'])->group(function () { Route::get('/', function () { // 使用 `first` 和 `second` 中间件 }); Route::get('user/profile', function () { // 使用 `first` 和 `second` 中间件 });});
命名空间
Route::namespace('Admin')->group(function () { // 在 "App\Http\Controllers\Admin" 命名空间下的控制器});
子域名路由
Route::domain('{account}.myapp.com')->group(function () { Route::get('user/{id}', function ($account, $id) { // });});
路由前缀
Route::prefix('admin')->group(function () { Route::get('users', function () { // 匹配包含 "/admin/users" 的 URL });});
表单伪造
<form action="/foo/bar" method="POST"> <input type="hidden" name="_method" value="PUT"> // 或者 {{ method_field('PUT') }} <input type="hidden" name="_token" value="{{ csrf_token() }}"></form>
获取当前路由信息
// Route::get('/', 'TestController@test')->name("mytest");$route = Route::current(); // object(Illuminate\Routing\Route)$name = Route::currentRouteName(); // mytest $action = Route::currentRouteAction(); // 控制器中:App\Http\Controllers\TestController@test 路由中:null
阅读全文
0 0
- laravel 5.5 -- router
- router
- Router
- router
- laravel 5.5 -- Eloquent 入门
- laravel 5.5 -- blade
- laravel 5.5 -- filesystem
- laravel 5.5 -- Cache
- laravel 5.5 -- Query Builder
- Laravel
- laravel
- Laravel
- laravel
- Laravel
- Laravel
- Laravel
- Laravel
- laravel
- 浅谈时间复杂度【帆帆帆笔记系列】
- 数据结构复习之–“2路归并排序”-JAVA实现
- 数学 随rand
- bzoj4586: [Usaco2016 Open]Landscaping
- 学习嵌入式Linux系统开发基础 第二课(Linux常用命令)
- laravel 5.5 -- router
- HDU 2844 Coins【多重背包】
- 使用下拉列表
- 面试题总结——JAV
- 【剑指offer】面试题7:两个栈实现队列
- 信息系统开发与管理--思维导图
- #include<filename.h> 和 #include"filename.h"有什么区别
- K:设计模式—绪论
- Historical Sales and Active Inventory kaggle