Error with complementary routes in Laravel 4

Asked

Viewed 572 times

1

Initially, I created the following route (1st search):

Route::get('/buscar/profissoes/{city_id_slug?}', array('as' => 'neighborhoods.city', 
    'uses' => 'NeighborhoodsController@getIndexCity'));

So the URL looks like this at the end:

 http://example.com/buscar/profissoes/1-uberlandia

After, I created the segund arota (2nd search - continuation)

Route::get('/buscar/profissionais/{city_id_slug}/{neighborhood_id_slug}/
{profession_id_slug}', array('as' => 'professionals.search', 'uses' => 'ProfessionalsController@getSearch'));

Then the URL should look like this:

 http://example.com/buscar/profissionais/1-uberlandia/15-santa-monica/1-pedreiro

But since the above URL is generated dynamically, initially it does not have the last 2 parameters. They will be added by clicking on Submit. Anyway, I added the parameters, just as a test and it didn’t work:

URL::route('professionals.search', array($city_id_slug, $city_id_slug, $city_id_slug))

Then Laravel returns the following:

Symfony \ Component \ Routing \ Exception \ RouteNotFoundException

Unable to generate a URL for the named route "http://example.com/buscar/profissionais/1-uberlandia/1-uberlandia/1-uberlandia" as such route does not exist.

What’s the right way to do this? And what is wrong in my routes, aiming that are different, and that even forcing the last 2 parameters with the number 1, did not work?

  • On the second route, the neighborhood_id_slug parameter could not, or should not, be optional({neighborhood_id_slug?}).

  • Yeah, I’ve done that too. But as I was doing a test "forcing" the parameters with the value 1, I made it mandatory to see if the same problem would happen.

3 answers

3


You can submit via POST all for the "fetch":

  cidade/{city_id_slug}/bairro/{neighborhood_id_slug?}/profissao/{profession_id_slug?} 

And redirect to a route GET with the URL handled.

I believe it is a smooth solution to be implemented. And it generates more control than you are doing.

  • I think it’s the best thing.

  • You will have more control over the route assembly in this way, passing the parameters according to your need.

1

Patrick,

Via Laravel it will not be possible to generate the second route correctly without the parameters.

I’ve had to do something like this before, and what I did was generate the first part (your route 1), and added manually via javascript (I no longer have access to the project, so I have no way to post the code to solve the problem).

Tip: The 2nd route must come first, because if it finds the "pattern" for first it will stop there.

  • The first route i Gero with javascript even. But as for the second, I could not find an alternative, I tried until this: URL::route('professionals.search', array($city_id_slug, 1, 1)) but it didn’t work.

  • In this pattern you will not be able to generate dynamically via Laravel, it must be via javascript itself, use the Laravel only for "root_url()".

1

Interpret each route as distinct, then you can create 3 routes: one that meets requests with 1 parameter, one with two and one with three...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.