What should CodeIgniter default controller be when implementing RESTful service? - php

I am using the CodeIgniter REST Server Library
https://github.com/philsturgeon/codeigniter-restserver
One problem about CodeIgniter is that it is not a RESTful framework. I get confused about the routes.php file that I have to set a "default controller."
$route['default_controller'] = "welcome";
What should I choose to be the default_controller if the server is fully RESTful?

add $route['(:any)'] = "your_default_controller/$1"; in your route config.
but it work for one controller. if you want to call another controller you still need including your controller at your URL http://localhost/api_rest/your_another_controller/method.

Related

Setting Routing rules for different classes in codeigniter

My application URL "localhost/crdlabs/PHP" goes to "localhost/crdlabs/home/display/PHP" which is done using route.php. The rule is as follows.
$route['(:any)'] = "home/display/$1";
Now that I have another controller class called displayarticles(). The current URL for this class is "localhost/crdlabs/displayarticles/article/learning-coding". I understand that I cannot use the routing above for the new controller. How to set a routing rule for the current one to make the URL look like "localhost/crdlabs/learning-coding".
Note : learning-coding part is dynamically set which means there are several different articles that should go to the same controller.
Any help/advise please.
The current routing rule.
$route['(:any)'] = "home/display/$1";
Will routes anything appearing after localhost/crdlabs/argument to localhost/crdlabs/home/display/argument.So the route localhost/crdlabs/learning-coding will be redirected to localhost/crdlabs/home/display/learning-coding.So you can not use like this.
But I suggest you to show your articles
localhost/crdlabs/displayarticles/article/learning-coding
To
localhost/crdlabs/articles/learning-coding
using the following routing rule.
$route['articles/(:any)'] = "displayarticles/article/$1";
Will be best.

Codeigniter 3.0 routing problems

I'm new in Codeigniter framework and got routing problem.
I have Main controller and Tournaments controller.
My routes look like this:
$route['default_controller'] = 'main';
$route['main'] = 'main';
$route['tournaments/results/(:num)'] = 'tournaments/results/$1';
When I go to localhost/tournaments/results/1 and then click to link "Main" I got localhost/tournaments/results/main instead of localhost/main
What is wrong with my routes or maybe problem is somewhere else?
Probably your link to 'Main' is wrong, not your routes files. Check your link.

codeigniter how do i setup routing for controller class and method?

I am new to CI and need some beginner's help from experts.
Here is what my current setup is:
/controllers/
home.php
report.php
/views/
home/index.php
home/recent.php
report/index.php
report/generate.php
the URI i am trying to produce as an outcome:
http://localhost
http://localhost/report (would load the index.php)
http://localhost/report/generate (would call the method for generate in the report controller)
http://localhost/recent/10 (would call the method for generate in the home controller passing the variable '10')
$route['default_controller'] = "home";
$route['404_override'] = '';
$route['/'] = 'home/index';
$route['recent/(:num)'] = 'home/recent/$1';
$route['report/(:any)'] = 'report/$1';
How do i avoid always modifying the routes file for each new method created in a class? so that it would follow:
$route[$controller/$method/$variable] (very use to how .net mvc routing is setup).
Any help is appreciated.
You don't need further modifications. In fact, even this line is redundant:
$route['report/(:any)'] = 'report/$1';
This one is also redundant:
$route['/'] = 'home/index';
since the default controller is set to 'home' and the default method is always index.
Look at how CI works with URLs: https://www.codeigniter.com/user_guide/general/urls.html
So, /localhost/report/generate would look for the Report controller, and load its generate method. That's how it works out-of-the-box, no routing needed.
And this route is fine:
$route['recent/(:num)'] = 'home/recent/$1';
If will take the URL /localhost/recent/123 and will load the Home, controller, recent method and will pass 123 as the first method parameter.

CodeIgniter turn off automatic routing?

Is it possible to turn off the automatic routing in CodeIgniter and have it only process requests if a route for that request exists? Thanks.
Keep in mind that Dale's solution:
$route['(:any)'] = "some/default/controller/$1";
only works for one-segment URLs, like:
example.com/foo
but not for:
example.com/foo/bar
You can get around this by using a regular expression instead of the CI wildcard. And by routing to a non-existing class drops a show_404() indeed:
$route['(.*)'] = "none";
AFAIK you can't turn off CI's automatic routing, but there is a work around:
// you specific routes
$route['admin/(:any)'] = "admin/$1";
$route['search/(:any)'] = "search/$1";
// the catch all route
$route['(:any)'] = "some/default/controller/$1";
Which doesn't actually turn off CI's routing but routes all unmatched uri's to the default controller.
Alternatively you could route to a non-existent controller which I believe will throw the in built 404 error
Well, another solution could be extends the Router.
Create a class name MY_Route at /application/core/MY_Router declared as class MY_Router extends CI_Router.
You could override the method _set_routing():
This function determines what should be served based on the URI request, as well as any "routes" that have been set in the routing config file.
It should be more complex, but at least can guide you to another solution.
Codeigniter 4 solution:
app/config/Routes.php at line 24 set value true to false
$routes->setAutoRoute(false);
No, it's not possible turn off the automatic routing convention in CodeIgniter as far as I know, but you can put entries in your .htaccess file to redirect de default routes to the routes you've created.

Default Controller in CodeIgniter

I am wondering if there is any other configuration options for a default controller.
For example - if I have a controller called "site" and I set the default controller in the following file: application/config/routes.php to:
$route['default_controller'] = "site";
I should be able to go to http://localhost and that brings up the
index(); function in the site controller.
However, if I try to do go to http://localhost/index.php/index2 to load the index2(); function I get a 404 error. If I change the URL to http://localhost/index.php/site/index2 it works fine - but I thought already set the default controller. Is there any way around this?
The only way to do it is manually writing the routing rule:
$route['index2'] = "site/index2";
You cannot do that.
The default_controller is only for URLs without any URI parameter. How could CodeIgniter distinguish between a method name and a controller name?
What you can do:
define a redirect inside your 404 document or directly map your 404 document to index.php
No CI doesn't work like that the first parameter has to be the name of a controller, so in this case you would have to create a controlled called "index2".

Categories