Laravel 路由数据重定向
Laravel route redirecting with data
我的基本路线如下所示:
Route::prefix('/group')->group(function () {
// Some routes here
Route::prefix('/{uuid}')->group(function () {
// Some routes here
Route::get('/user/{id}', 'Controller@preview')->name('view-user')->where('id', '[0-9]+');
}
}
逻辑是我希望 id
只是数值。我现在想做的是,如果该值是非数字的,则声明对此的重定向。假设 id
的输入是 fs
。在那种情况下,我希望它重定向到 id
,值为 1
。
我尝试使用 Route:redirect
,但无法正常工作。它看起来像这样:
Route::redirect('/group/{uuid}/user/{id}', '/group/{uuid}/user/1')->where('id', '[^0-9]+');
我更愿意将重定向放在组内,但如果这是唯一的方法,也可以放在组外。任何帮助将不胜感激。
发生的情况是,如果我声明了路由重定向,我会收到 404 错误。
编辑:我想在routes/web.php
文件中进行。我知道如何在控制器中执行此操作,但在当前情况下这不是我所需要的。
闭包也不是一种选择,因为那样会阻止路由缓存。
你声明它倒置了。
在 Laravel 中,您可以通过这种方式重定向传递的参数:
您可以传递名称而不是 url 并简单地传递变量。
Redirect::route('view-user', [$uuid, $id])
我认为你可以在路由器的控制器内部完成,逻辑如下:
class Controller {
// previous code ..
public function preview($uuid, $id) {
if(! is_numeric($id))
return redirect("/my-url/1");
// run the code below if $id is a numeric value..
// if not, return to some url with the id = 1
}
}
我认为没有办法覆盖 laravel 的 'where' 功能,但我想在路由绑定中有类似的东西:
Alternatively, you may override the resolveRouteBinding method on your Eloquent model. This method will receive the value of the URI segment and should return the instance of the class that should be injected into the route:
/**
* Retrieve the model for a bound value.
*
* @param mixed $value
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function resolveRouteBinding($value)
{
return $this->where('name', $value)->first() ?? abort(404);
}
但这要求您管理简洁模型的值,而不是您想要的任何 ID。
在路由中指定路由名称。
return Redirect::route('view-user', ['uuid'=>$uuid,'id'=>$id]);
然后在 web.php 文件中随心所欲。
Route::get('/group/{uuid}/user/{id}', function($uuid, $id){
echo $uuid;
echo $id;
})->name('view-user')->where('id', '[0-9]+');
跟进评论
You can create a Route in routes/web.php file that catches non-digit ids and redirect this to 'view-user' with id=1
看起来像这样
Route::get('/group/{uuid}/user/{id}', function ($uuid, $id) {
return redirect('view-user', ['uuid' => $uuid, 'id' => 1]);
})->where('id', '[^0-9]+');
// and then below have your normal route
Route::get('/group/{uuid}/user/{id}', 'Controller@preview')->name('view-user')->where('id', '[0-9]+');
更新
关注你的评论说你不想使用闭包。
将“错误的输入路由”更改为
Route::get('/group/{uuid}/user/{id}', 'Controller@redirectBadInput')->where('id', '[^0-9]+');
然后在class中添加方法 Controller
:
public function redirectBadInput ($uuid, $id) {
return redirect('view-user', ['uuid' => $uuid, 'id' => 1]);
}
您可以在 中查看有关重定向和缓存的更多信息。
我的基本路线如下所示:
Route::prefix('/group')->group(function () {
// Some routes here
Route::prefix('/{uuid}')->group(function () {
// Some routes here
Route::get('/user/{id}', 'Controller@preview')->name('view-user')->where('id', '[0-9]+');
}
}
逻辑是我希望 id
只是数值。我现在想做的是,如果该值是非数字的,则声明对此的重定向。假设 id
的输入是 fs
。在那种情况下,我希望它重定向到 id
,值为 1
。
我尝试使用 Route:redirect
,但无法正常工作。它看起来像这样:
Route::redirect('/group/{uuid}/user/{id}', '/group/{uuid}/user/1')->where('id', '[^0-9]+');
我更愿意将重定向放在组内,但如果这是唯一的方法,也可以放在组外。任何帮助将不胜感激。
发生的情况是,如果我声明了路由重定向,我会收到 404 错误。
编辑:我想在routes/web.php
文件中进行。我知道如何在控制器中执行此操作,但在当前情况下这不是我所需要的。
闭包也不是一种选择,因为那样会阻止路由缓存。
你声明它倒置了。
在 Laravel 中,您可以通过这种方式重定向传递的参数:
您可以传递名称而不是 url 并简单地传递变量。
Redirect::route('view-user', [$uuid, $id])
我认为你可以在路由器的控制器内部完成,逻辑如下:
class Controller {
// previous code ..
public function preview($uuid, $id) {
if(! is_numeric($id))
return redirect("/my-url/1");
// run the code below if $id is a numeric value..
// if not, return to some url with the id = 1
}
}
我认为没有办法覆盖 laravel 的 'where' 功能,但我想在路由绑定中有类似的东西:
Alternatively, you may override the resolveRouteBinding method on your Eloquent model. This method will receive the value of the URI segment and should return the instance of the class that should be injected into the route:
/**
* Retrieve the model for a bound value.
*
* @param mixed $value
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function resolveRouteBinding($value)
{
return $this->where('name', $value)->first() ?? abort(404);
}
但这要求您管理简洁模型的值,而不是您想要的任何 ID。
在路由中指定路由名称。
return Redirect::route('view-user', ['uuid'=>$uuid,'id'=>$id]);
然后在 web.php 文件中随心所欲。
Route::get('/group/{uuid}/user/{id}', function($uuid, $id){
echo $uuid;
echo $id;
})->name('view-user')->where('id', '[0-9]+');
跟进评论
You can create a Route in routes/web.php file that catches non-digit ids and redirect this to 'view-user' with id=1
看起来像这样
Route::get('/group/{uuid}/user/{id}', function ($uuid, $id) {
return redirect('view-user', ['uuid' => $uuid, 'id' => 1]);
})->where('id', '[^0-9]+');
// and then below have your normal route
Route::get('/group/{uuid}/user/{id}', 'Controller@preview')->name('view-user')->where('id', '[0-9]+');
更新
关注你的评论说你不想使用闭包。
将“错误的输入路由”更改为
Route::get('/group/{uuid}/user/{id}', 'Controller@redirectBadInput')->where('id', '[^0-9]+');
然后在class中添加方法 Controller
:
public function redirectBadInput ($uuid, $id) {
return redirect('view-user', ['uuid' => $uuid, 'id' => 1]);
}
您可以在