简体   繁体   中英

How to make a route with different controllers and the same URL's with laravel 4?

I have these links in my view game.blade.php :

<a href="{{ URL::route('checkFirstName', $item->PK_item_id) }}"></a> 

<a href="{{ URL::route('checkSecondName', $item->PK_item_id) }}"></a>  

And these routes in mu routes.php file :

Route::get('/game/{itemId}', array('as' => 'checkFirstName', 'uses' => 'GameController@checkFirstName'));
Route::get('/game/{itemId}', array('as' => 'checkSecondName', 'uses' => 'GameController@checkSecondName'));

and these methods in my GameController.php :

public function checkFirstName($itemId)
{
    dd('check first name from ' . $itemId);

}

public function checkSecondName($itemId)
{
    dd('check second name from ' . $itemId);

}

The problem :

Both links are going to the checkSecondName() function.

Your routing design is just wrong - both your routes match the same path, so what happens here is that the second one is overwriting the first one. Try using different paths, or maybe use only one controller if the same functionality will be provided by both controllers. You just cannot have two different routes matching the same path.

Thing is, wheather you call...

URL::route('checkSecondName', $item->PK_item_id)

or...

URL::route('checkFirstName', $item->PK_item_id)

...Laravel will generate the same URL path , which is - /game/{itemId} . Named routes exist for convenience. In the end what's important is the path specified in Route declaration.

So, what happens is Laravel checks the path to find matching route, but in your case there's two matches. The last one is chosen by design.

What this should tell you is pretty straightforward: you cannot have the same route calling different controller methods. What can be different is the verb used: Route::get('/game/{itemId}') is not the same as Route::post('/game/{itemId}') , but that's just a sidenote.

What could be done here is eg having an extra param to determine a type of action to be done:

Route

Route::get('/game/{itemId}/{type}', array('as' => 'checkName', 'uses' => 'GameController@checkName'));

HTML

<a href="{{ URL::route('checktName', ['itemId' => $item->PK_item_id, 'type' => 'first']) }}"></a> 

<a href="{{ URL::route('checktName', ['itemId' => $item->PK_item_id, 'type' => 'last']) }}"></a> 

Controller

public function checkName($itemId, $type)
{
    if ($type === 'first') {
        // first name handling        
    } else {
        // last name handling
    }
}

The answer of lessugar was correct. I have also found another solution to this so I thought I should add this here too.

changing the routes to :

Route::get('/game/{itemId}_first', array('as' => 'checkFirstName', 'uses' => 'GameController@checkFirstName'));
Route::get('/game/{itemId}_second', array('as' => 'checkSecondName', 'uses' => 'GameController@checkSecondName'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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