Dynamic routing

I want to be able to choose a controller based on data gathered form the uri.

I have a categories table and a subcategories table. Basically I have a URL in the following format (:any)/(:any). The first wildcard is a city slug (i.e edinburgh) and the second is going to be either a category or a subcategory slug.

So in my route I search for categories with that route, if I find it, I want to use controller: forsale and method: get_category. If it’s not a category I’ll look up subcategories, if I find it in there I want to use controller: forsale and method: get_subcategory. If it’s not a subcategory I want to continue looking for other routes.

Route::get('(:any)/(:any)', array('as'=>'city_category', function($city_slug, $category_slug){
    // is it a category?
    $category = Category::where_slug($category_slug)->first();
    if($category) {    
        // redirect to controller/method
    } 

    // is it a subcategory?
    $subcategory = Subcategory::where_slug($category_slug)->first();
    if($subcategory) {
        // redirect to controller/method
    }
    // continue looking for other routes
}));

First off I’m not sure how to call a controller/method here without actually redirecting (thus changing the url again).

And secondly, is this even the best way to do this? I started using /city_slug/category_slug/subcategory_slug. But I want to only show city_slug/category|subcategory_slug but I need a way to tell which the second slug is.

Lastly, there may be other URL’s in use that follow (:any)/(:any) so I need it to be able to continue looking for other routes as well.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Answer to your questions in order:
1. Instead of using different controller#action‘s you could use a single action and based on the second slug (category or subcategory), render a different view (although I don’t like this approach, see #2 and #3):

public class Forsale_Controller extends Base_Controller {
  public function get_products($city, $category_slug) {
    $category = Category::where_slug($category_slug)->first();
    if($category) {    
      // Do whatever you want to do!
      return View::make('forsale.category')->with(/* pass in your data */);
    }

    $subcategory = Subcategory::where_slug($category_slug)->first();
    if($subcategory) {
      // Do whatever you want to do!
      return View::make('forsale.sub_category')->with(/* pass in your data */);
    }
  }
}

2. I think /city_slug/category_slug/subcategory_slug is way better than your method! You should go with this one!!
3. Again, you should revise your routes. I always try to make my routes in a way that they don’t confuse me, neither Laravel!! Something like /products/city/category/subcategory is much more clear!

Hope it helps (my code is more like a psudocode, it’s not been tested )!


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x