Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9140739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:33:20+00:00 2026-06-17T09:33:20+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:33:21+00:00Added an answer on June 17, 2026 at 9:33 am

    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 )!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to choose between .mouseover and .click events based on
I want to put a form in my menu to be able to choose
I want the user to be able to choose from a dropdown Combobox listing
I want to be able to take an image that i have already captured
I want to be able to do the following actions with a form submit
I want to be able to hide a selector if it contains any data.
I have 3 drop-down boxes, which work great. I'm able to choose the values
I want to be able to choose an nth child from a list of
I want to be able to choose from a list of fogbugz issues when
I have an asp.net mvc application where i want the user to be able

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.