Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 739
Martin Dimitrov Avatar asked Nov 28 '19 11:11

Martin Dimitrov


People also ask

How do I redirect a route with data?

Redirect with Data Firstly, you can just use with(): return redirect()->back()->with('error', 'Something went wrong. '); This code will add an item to the Session Flash Data, with key “error” and value “Something went wrong” – and then you can use that in the result Controller or View as session('error').

Can we send data with redirect in Laravel?

$data=['id'=>1,'name'=>'test']; return Redirect::route('Controller. method')->with('data' => $data);

How do you pass a variable redirect in Laravel?

You can use the compact function to pass it to your view and reference it by the name. Show activity on this post. return view('viewfile')->with('card',$card)->with('another',$another); You can send data using redirect method.

How do I redirect in Laravel?

Redirecting to Controller Actions Not only named route but we can also redirect to controller actions. We need to simply pass the controller and name of the action to the action method as shown in the following example. If you want to pass a parameter, you can pass it as the second argument of the action method.


1 Answers

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.

like image 173
ege Avatar answered Oct 07 '22 17:10

ege