简体   繁体   English

Laravel 路由重定向数据

[英]Laravel route redirecting with data

I have a basic route that looks like this:我有一个看起来像这样的基本路线:

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]+');
    }
}

The logic is that I want the id to be only numerical value.逻辑是我希望id只是数值。 What I want to do now is, to declare a redirection to this, if the value is non-numerical.我现在想做的是,如果该值是非数字的,则声明对此的重定向。 Let's say the input of id is fs .假设id的输入是fs In that case I would want it to redirect to id with value 1 .在那种情况下,我希望它重定向到值为1id

I tried using Route:redirect , but could not make it work.我尝试使用Route:redirect ,但无法正常工作。 It looks something like this:它看起来像这样:

Route::redirect('/group/{uuid}/user/{id}', '/group/{uuid}/user/1')->where('id', '[^0-9]+');

I would prefer to put the redirect inside the groups, but it can be outside if this is the only way.我更愿意将重定向放在组内,但如果这是唯一的方法,它也可以放在组外。 Any help will be greatly appreciated.任何帮助将不胜感激。

What happens is, that I get a 404 error if I have the route redirect declared.发生的事情是,如果我声明了路由重定向,我会收到 404 错误。

EDIT : I want to do it in the routes/web.php file.编辑:我想在routes/web.php文件中进行。 I know how to do it in the controller, but it is not what I need in the current case.我知道如何在控制器中执行此操作,但在当前情况下这不是我所需要的。 Closures are not an option either, because that would prevent routes caching.闭包也不是一种选择,因为那样会阻止路由缓存。

You declared it inverted.你宣布它倒置了。

In Laravel you can redirect passing parameters in this way:在 Laravel 中,您可以通过以下方式重定向传递的参数:

You can pass the name instead of the url and simply pass variables.您可以传递名称而不是 url 并简单地传递变量。

Redirect::route('view-user', [$uuid, $id])

I think that you can do it inside of the controller of the router, with a logic like this:我认为您可以在路由器的控制器内部执行此操作,逻辑如下:

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
    }
}

I think that there is no way to override the 'where' function of laravel, but I guess that have something like that in Routing Bindings:我认为没有办法覆盖 laravel 的“where”功能,但我想在路由绑定中有类似的东西:

Alternatively, you may override the resolveRouteBinding method on your Eloquent model.或者,您可以覆盖 Eloquent 模型上的 resolveRouteBinding 方法。 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:此方法将接收 URI 段的值,并应返回应注入路由的类的实例:

/**
 * 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);
}

But it's require that you manage consise model's values instead of ids of whatever you want.但这要求您管理 consise 模型的值而不是您想要的任何 id。

Following up on the comment跟进评论

You can create a Route in routes/web.php file that catches non-digit ids and redirect this to 'view-user' with id=1您可以在 routes/web.php 文件中创建一个路由来捕获非数字 ID,并将其重定向到 id=1 的“view-user”

It would look something like this它看起来像这样

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]+');

Update更新

Following you comment that you do not want to use closures.在您评论说您不想使用闭包之后。

Change the "bad input route" to将“错误的输入路径”更改为

Route::get('/group/{uuid}/user/{id}', 'Controller@redirectBadInput')->where('id', '[^0-9]+');

and then add the method in class Controller :然后在类Controller中添加方法:

public function redirectBadInput ($uuid, $id) {
  return redirect('view-user', ['uuid' => $uuid, 'id' => 1]);
}

You can see more in this SO thread about redirects and caching.您可以在此 SO 线程中看到有关重定向和缓存的更多信息。

assign route name in route as like.像这样在路线中分配路线名称。

return Redirect::route('view-user', ['uuid'=>$uuid,'id'=>$id]);

As you want in web.php file then.然后在 web.php 文件中如你所愿。

Route::get('/group/{uuid}/user/{id}', function($uuid, $id){
    echo $uuid;
    echo $id;
})->name('view-user')->where('id', '[0-9]+');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM