Welcome Guest, Not a member yet? Register   Sign In
n00b 15 minutes with codeigniter questions on urls, get parameters and multi-environments
#1

[eluser]Moobies[/eluser]
hi i've decided to give codeigniter a spin for a project. i am 15 minutes in and am struggling with a couple of bits and wondered if the community could help as i could not find anything in the docs.

1. I want my Controller URL to be apples-and-pears. What should the class php file be called and what should the class name be. I have tried applesandpears.php, apples_and_pears.php and apples-and-pears.php with combinations of class names like ApplesAndPears, Applesandpears and I am just not getting this right.

2. I have a Controller that will be an event handler, so it's called handler. I am only interested in it being hit via /handler on my site, so I implement the login in the index() function. I like that /hander routes to Handler->index(). However I need to have get data sent to this URL, e.g. /hander?foo=bar. I would expect in my Handler->index() to be able to do $this->input->get('foo'). But I am told /handler?foo=bar does not exist. Can CI really not handle a perfectly mapped Controller and action function and then accept QS params? Do I really have to choose between a nice and an ugly URL syntax? Seems limited unless there is a way (of course I can use POST but I don't want to - it's semantically incorrect).

3. What is the easiest way to go about multi-configs per server. We have a dev, stage and live setup. I see there is a system/application/config/config.php. How can I branch this per server environment? The base_url at the very least will change and database credentials.

Thanks for your time! I hope nothing too taxing for you pros!
#2

[eluser]Vicente Russo[/eluser]
[quote author="Moobies" date="1240418833"]1. I want my Controller URL to be apples-and-pears. What should the class php file be called and what should the class name be. I have tried applesandpears.php, apples_and_pears.php and apples-and-pears.php with combinations of class names like ApplesAndPears, Applesandpears and I am just not getting this right.[/quote]

You cant use "-" on controller name. You can use parameter with "-", though. If you want to use site.com/apples-and-pears you can use _remap function on your default controller.

[quote author="Moobies" date="1240418833"]2. I have a Controller that will be an event handler, so it's called handler. I am only interested in it being hit via /handler on my site, so I implement the login in the index() function. I like that /hander routes to Handler->index(). However I need to have get data sent to this URL, e.g. /hander?foo=bar. I would expect in my Handler->index() to be able to do $this->input->get('foo'). But I am told /handler?foo=bar does not exist. Can CI really not handle a perfectly mapped Controller and action function and then accept QS params? Do I really have to choose between a nice and an ugly URL syntax? Seems limited unless there is a way (of course I can use POST but I don't want to - it's semantically incorrect).[/quote]

If I understood correctly, you can use the sampe _remap function to do this. Remap functions can accept parameters and get uri segments.

[quote author="Moobies" date="1240418833"]3. What is the easiest way to go about multi-configs per server. We have a dev, stage and live setup. I see there is a system/application/config/config.php. How can I branch this per server environment? The base_url at the very least will change and database credentials.[/quote]

Found two URLs on forum: link - link. Maybe it's what you are looking for.
#3

[eluser]Moobies[/eluser]
thanks, i'll take a look at this remap function, though i can't understand why the controller cannot contain hyphens as it's a valid url syntax and afterall the controller will still be the first url segment.

regards multi-env. config, your links are about multi-apps on 1 server. my issue is more related to running the same app on multiple server environments.

i have a custom solution right now. i create my own config file in config/ and then inside that:

Code:
$server_configs = array(
  'dev.server' => array('foo' => 'bar1'),
  'tst.server' => array('foo' => 'bar2'),
  'prd.server' => array('foo' => 'bar3')
);

$config['myapp'] = $server_configs[$_SERVER['SERVER_NAME']];
#4

[eluser]Colin Williams[/eluser]
I think you might want to think about REST. You apples-and-pears URI is fine, but your controller is likely a Fruit controller. So the system URI is something like /fruit/category/apple/pear, then you can use routing to achieve the custom /apples-and-pears URI.

Quote:Do I really have to choose between a nice and an ugly URL syntax?

Well, you can, and I think should, abandon query strings altogether and instead adopt key/value pairs. So, /handler?foo=bar becomes /handler/foo/bar. CI's URI class has functions for automatically getting these pairs as an associative array. Look at this page's URI, /forums/viewthread/112325, which could possibly be /forums?viewthread=112325. It's just a new style you either choose to adopt or not.




Theme © iAndrew 2016 - Forum software by © MyBB