Victordb
Victordb

Reputation: 549

Laravel only one of two routes accessible at a time

If I have two routes A and B in my web.php. How can I make a middleware check like this:

if (something in my database have status == 0) {
    return $next($request); ROUTE A
} else {
    return $next($request); ROUTE B
}

I mentioned return $next($request); because there is one condition in this, I don't want the routes to be both accessible at the same time. So for example if I go to url/routeA if the condition is met allow if not show route B. The same goes for route B.

The logic would be like this users chooses an item from a list (in the db the list is represented like this: list with id,name,status) if the status is 0 show route A if status is 1 show route B. If I try to visit url/routeB?listID1 directly check condition (for id1) and show either route A or B.

Only 0 or 1 in the status. If the status is 1 all users would be accessing routeB directly, if the status is 0 the first users to get there would access routeA do some action (that would set the status to 1) get redirected to routeB. Next users after him will reach routeB directly

Upvotes: 0

Views: 1352

Answers (2)

JPark
JPark

Reputation: 789

If you want, you can just do a redirect from your middleware:

if (something in my database have status == 0) {
    return redirect()->route('route_a_name');
} else {
    return redirect()->route('route_b_name');
}

Just make sure you name your routes in your routes/web.php.

Example:

Route::get('/route_a', 'YourController@RouteA')->name('route_a_name');

Edit:

Based on the additional info you provided, a middleware for what you are trying to achieve is kind of over the top. Instead you could simply use a single route and pass in the status parameter. For more info on routing parameters see https://laravel.com/docs/5.6/routing#route-parameters.

Example:

Route::get('/your_route/{status}', 'YourController@ControllerAction');

Then in your controller, handle the logic based on the status:

function ControllerAction(Request $request, $status) {
    if ($status == 0) {
        //Show view for A
    } else {
        //Show view for B
    }
}

Upvotes: 2

Md Nasir Fardoush
Md Nasir Fardoush

Reputation: 820

You can use route name and simply use it any where.

  //define route
  Route::get('/A', 'ControllerA@methodA')->name('A');
  Route::get('/B', 'ControllerB@methodB')->name('B');

In your controller simply use route name

   $routeName = ['A','B']; //route name
   If (something in my database have status == 0) {
     return Redirect::route($routeName[0]); //ROUTE A
   } else {
      return Redirect::route($routeName[1]); //ROUTE B
   }

Otherwise you can do something getting route group parameter

  Route::group(['prefix' => '/test/{id}'], function() {
   //you can't access parameter directly here 
    $id = explode('test/',url()->current())[1]; //get parameter with different way
    if($id == 1){
       exit('do something');
    }else{
        exit('do others');
    }
 });

Upvotes: 4

Related Questions