5

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. 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. In that case I would want it to redirect to id with value 1.

I tried using Route:redirect, but could not make it work. 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.

EDIT: I want to do it in the routes/web.php file. 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.

8
  • 1
    You can use Regular Expression Constraints to only allow numerical values
    – user8034901
    Commented Nov 28, 2019 at 11:37
  • @kerbholz That is indeed a good idea, but in my case it would not work, as in order to cache the routes, there cannot be closures. Commented Nov 28, 2019 at 11:38
  • Use a ternary with regular expression in where clause maybe? Commented Nov 28, 2019 at 11:49
  • 1
    You can create a Route in routes/web.py that catches non-digit ids and redirect this to 'view-user' with id=1
    – ege
    Commented Nov 28, 2019 at 12:12
  • 2
    @ege This is exactly what I am trying to do, but I don't know how. If you could give a code sample it will be greatly appreciated. Commented Nov 28, 2019 at 12:17

4 Answers 4

3

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

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:

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.

4
  • Yes, but sadly, I cannot use closures in this due to caching Commented Nov 28, 2019 at 12:53
  • So add it as a method of Controller? ie. 'Controller@redirectBadInput'
    – ege
    Commented Nov 28, 2019 at 12:54
  • See this about caching with redirects: stackoverflow.com/questions/35613579/…
    – ege
    Commented Nov 28, 2019 at 12:57
  • Maybe this is the only way to do it after all Commented Nov 28, 2019 at 13:09
3

You declared it inverted.

In Laravel you can redirect passing parameters in this way:

You can pass the name instead of the url and simply pass variables.

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

1
  • I am not asking how to make a redirect, but how to declare a redirection rule in web.php file. I want the entire logic to be in the routes/web.php file, not in the controller. Commented Nov 28, 2019 at 11:42
3

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:

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

But it's require that you manage consise model's values instead of ids of whatever you want.

1
  • I have updated again the question. I had stated before in a comment below, closures are not an option for me. I have now put it in the question. Commented Nov 28, 2019 at 12:05
1

assign route name in route as like.

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

As you want in web.php file then.

Route::get('/group/{uuid}/user/{id}', function($uuid, $id){
    echo $uuid;
    echo $id;
})->name('view-user')->where('id', '[0-9]+');
2
  • As I stated before, I have to put it in routes/web.php, not in the controller and also closures are not an option for me Commented Nov 28, 2019 at 12:10
  • As I said, closures are not an option here Commented Nov 28, 2019 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.