Welcome Guest, Not a member yet? Register   Sign In
Dynamic Controller Names
#1

[eluser]PHP Creative[/eluser]
Hi there

I’m not sure how to explain this. Below is an example of the URL I am using. As you can see there are 2 controller names, ‘restaurants’ and ‘info’. However the URL is not ideal because of the ‘info’ controller.

http://www.example.com/restaurants/info/Barrys-grill

The ideal URL is shown below:

http://www.example.com/restaurants/Barrys-grill

As you can see ‘Barrys-grill’ is a controller name but because I could have hundreds of names writing individual controllers in the code isn’t ideal. Is it possible to write dynamic controller names?

I have reading the User Guide and so far I haven't had any luck regarding this. Could someone point me in right direction.
#2

[eluser]Thorpe Obazee[/eluser]
Code:
$routes['restaurants/(\S+)'] = 'restaurants/info/$1';

If I understand what you mean, you'll probably use this route.
#3

[eluser]PHP Creative[/eluser]
Thanks very much. I understand that now Smile
#4

[eluser]tomcode[/eluser]
You can also use a _remap() method in Your Controller which catches all incoming requests. See the User Guide / Controllers.
#5

[eluser]jedd[/eluser]
[quote author="PHP Creative" date="1242628184"]Hi there

I’m not sure how to explain this. Below is an example of the URL I am using. As you can see there are 2 controller names,[/quote]

Hi PHP Creative. Actually, it's non-obvious that you have 2 controller names from the URL you provided - it relies on knowing where you installed the CI code to.

Quote:http://www.example.com/restaurants/info/Barrys-grill

The ideal URL is shown below:

http://www.example.com/restaurants/Barrys-grill

As you can see ‘Barrys-grill’ is a controller name ...

Barrys-grill shouldn't be a controller (name) at all. It should be the parameter you pass to your Restaurants() controller.

Your concern about having hundreds of names writing individual controllers suggests that you haven't got a completely clear idea of how the URL is parsed and handed over to the CI infrastructure.

This is covered pretty well in the [url="http://ellislab.com/codeigniter/user-guide/general/controllers.html#passinguri"]CI User Guide section on controllers - passing URI Segments section[/url].

I'd suggest you have a re-read through that bit.
#6

[eluser]PHP Creative[/eluser]
Hi there.

Thanks a million for the replies. I think the routes option is the best but the function remaping solution would be fine apart from the fact I have a hundreds of names in the table.

Code:
function _remap($method)
{
    if ($method == 'resturant_name_here')
    {
        $this->info();
    }
    else
    {
        $this->index();
    }
}

Does anybody know of a solution around this that doesn't require checking the db for the names?
#7

[eluser]BDKR[/eluser]
[quote author="PHP Creative" date="1243026916"]
Does anybody know of a solution around this that doesn't require checking the db for the names?
[/quote]

A dictionary? Of course, the next question is "How do you populate the dictionary"? LOL
#8

[eluser]Dam1an[/eluser]
You can either try to match it to a value in the database, or assume it must be name if it isn't a function in this controller (can check this using method_exists())
#9

[eluser]Ki[/eluser]
I have had a similar problem and i have solved it with editing routes.php in your config folder to forward anything after ...restaurants/ to restaurants controller, which only has one method - index($restaurant_name). So, if you type restaurants/my_grill or restaurant/johns_grill, it will be passed to controller restaurant, method index($restaurant_name) with the variable $restaurant_name catching anything in the url after restaurant/. Further, within the index, if you are using database, you can use the IF(){} to see if the $restaurant_name matches a name in database and display the menu, or else show view that says something like "NO RESTAURANT FOUND". Hope that helps, if I understood correctly.




Theme © iAndrew 2016 - Forum software by © MyBB