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".
Related
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.
My current url is universitytwig.com/user/userprofile/cm.cm , where user is the controller , userprofile is the method and cm.cm is the parameter .How i change this url to universitytwig.com/cm.cm by route.php or by routing from database, Or by .htaccess
Try to add
$route['cm.cm'] = 'user/userprofile/cm.cm';
to your application/config/routes.php
Or
$route['(:any)'] = 'user/userprofile/$1';
if your parameter can be different.
you can use the routes.php but if you make a rule in something like cm.cm to user/userprofile/cm.cm
$route['(:any)']= 'user/userprofile/$1';
But this is not a perfect solution it will point all the controllers of every thing after base url to user/userprofile/ you need to specify some string the CI routes will identify that this needs to be point on this controller if cm.cm is dynamic parameters not hard coded for this your url structure should be
universitytwig.com/students/cm.cm
and in routes you can now make a rule
$route['students/(:any)']= 'user/userprofile/$1';
This will work for your scenario
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.
I am working with CodeIgniter. Here's my routing file
$route['default_controller'] = 'pages/view/home';
$route['(:any)'] = 'pages/view/$1';
where
pages is the controller class and view is a function of it and home is a parameter to that function.
Now, this is the default controller. When i need to open someother page rather than 'home' I do it like as follows from inside a view
href="<?php echo base_url('products');?>
Now what i want to ask is, if i create a new controller, how can i use the function of that controller? since I am only passing the third parameter to the base_url() function.
Obviously I think I gotta write $routes, but how ? since all the traffic is passed to
pages/view
I tried creating a new controller but couldn't be able to use it. My new contoller was name new_controller and it has a function call new_function()
and I wrote the $route as follows
$route['pages/view/product'] = 'new_controller/new_function';
You shouldn't have to worry about Routes if you take away the (:any) route you have place there. That is blocking all other controllers from being loaded, I think.
If you have a controller called "Stuff"
in your URL when you have mysite.com/stuff/foo/param Code Igniter should bypass the default "page" controller and use
I think you would be better off doing something like this
$route['page/(:any)'] = "page/view/$1";
And change your default to be only 'pages'
That would open up your new controller to be used in the normal codeigniter fashion
In CodeIgniter the routes are evaliated in row, so first you have the default route and after that sould you place the new route, $route['pages/view/product'], if you want to keep the (:any) route, and with this, you place the exceptional routes before the (:any) route.
In my routes.php I have :
$route['default_controller'] = "bitcoin";
and in my config.php I have:
$config['base_url'] = 'http://localhost/bitcoin/';
$config['index_page'] = 'index.php';
here is my controller: http://www.pastie.org/2253458
When I try to go to the following, I get a 404 not found (but the 404 template looks different):
http://localhost/bitcoin/edit/
http://localhost/bitcoin/index.php/edit
You can't access functions through the default controller like this.. It's assumes your trying to access another controller. Default controller is used when nothing is passed, ie: index.php
You will need to go to /bitcoin/index.php/bitcoin/edit
And note you will only be able to go to /bitcoin/bitcoin/edit if you have a htaccess file setup for routing.
You didn't say if you've removed or not your index with .htaccess, but if you didn't, did you try using: http://localhost/index.php/bitcoin ?
Or better, since it's your default controller, just http://localhost ?
What you're doing is quite strange, I can't understand if you're in a subfolder called bitcoin (in case, it should be http://localhost/bitcoin/ to call the default controller, which is also called bitcoin but doesn't need to be indicated in your URL).
If you're in root, You should rewrite your urls as: http://localhost/index.php/bitcoin/edit to call the edit() method of your default controller
Edit:
If you're in a subfolder called bitcoin, your base url, with default controller, should be:
http://localhost/bitcoin/ (which is the same as http://localhost/bitcoin/index.php/bitcoin)
If you want to get the bitcoin method edit(), should be http://localhost/bitcoin/index.php/bitcoin/edit
Also, try removing your .htaccess AT ALL and see what happens.
Edit2
Oh, one last thing: use CI_Controller and not CI_controller, if you're on a OS where lowercase matters you might encounter some problems