Jon Jon - 1 year ago 125
PHP Question

Laravel - Passing two variables to controller makes another page break

I have encountered a problem that has really confused me. I've been trying to fix it, but the way that works, I don't want.

So, I passing two variables to my ThreadController; 'category' and 'threadTitle' in the following way:

Route::get('/{category}/{threadTitle}', 'ThreadController@viewThread');

Just for testing purposes, I'm outputting the passed through variables out to the web page.

{{ $category.$threadTitle }}

Everything works fine, they're outputted. Unfortunately, this also breaks my new thread page, which routes are the following:

Route::group(['prefix' => 'thread'], function() {
Route::get('/new', function() {
return view('thread/new');

Route::get('/new', 'CategoryController@listCategoriesOnAddThread');
Route::post('/new', 'CategoryController@addThread');

On the new thread page, the routes are outputted instead of the form. So, 'threadnew' is shown.

I did have a fix where I changed my category route to:

Route::get('/category/{category}/{threadTitle}', 'ThreadController@viewThread');

That works fine, nothing breaks. However, I don't want to use that way, I'd rather just have {category}/{threadTitle}

I'm really stuck right now and would appreciate any help.

Answer Source

The problem is that you have a general route placed before the specific ones, that catches every url with two segments. Just place the

Route::get('/{category}/{threadTitle}', 'ThreadController@viewThread');

below the specific routes and everything should work fine.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download