Using any other controller class rather than the default controller class - php

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.

Related

Does CodeIgniter always associate a controller to a segment of a URI?

This is my first attempt to work/learn CodeIgniter. However, I'm struggling in understanding the "C".
1) Does CodeIgniter always associate a controller to a segment of a URI?
2) What are the best practices to work with controllers? I mean, how can I avoid dumping all my methods in a single controller? Can I split a controller in several files without creating unnecessary URI.
1.Yes controller always associate to segment of a URI. If your controller is under some directory like
controllers
search ---------------------directory inside controller
search ------------------controller
stock_search -------------------method
then it will add whole path in the uri segment e.g :basepath.'search/search/stock_search/';
But you can route it your custom path using routes.php
$route['search'] = 'search/search/stock_search/';
2.You can create different controllers (name should be different) with the different methods or you can say you can split controller methods in different files and customise their url accordingly in routes.php and can create parent controller to use methods in any controller by extending through it.
If you want to get something in codeigniter then codeigniter send the request to a controller. URI must have a controller if no controller in uri then the reguest is goes to default controller which is tell in application/config/routes.php in this code $route['default_controller'] = 'welcome';
And will not be able to split a controller in several files without creating more than one URI.
Controller is associated to url segments.
Url used in Codeigniter is as follows: http://example.com/index.php/projname/controller/method/params.
If you dont specify controller in uri, default controller is called specified in routes.php $route['default_controller'] = 'welcome';

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 Routing

I'm trying to write an application that takes a parameter from the url and supplies it to the index function in the default controller. Then decides what to load depending on this parameter either a default home page or a custom page.
$route['(eventguide/:any)'] = 'eventguide';
This code works but only if I have the controller in the url like so:
example.com/eventguide/(parameter)
I don't want to include the controller name. So i'm not exactly sure how to route this.
Ideally the url would look like example.com/(parameter), is this possible?
Yes, you're almost there:
$route['(:any)'] = "eventguide/index/$1";
And in your index() method you fetch the parameter:
public function index($parameter = null){
}
$parameter will now contain anything caught by the :any shortcut, which should be equivalent to (\w)+ IIRC
Since this is a catch-all route, be careful to put any other custom route you want before it, otherwise they will never be reached.
For ex., if you have a controller "admin", your route files should be;:
$route['admin'] = "admin";
$route['(:any)'] = "eventguide/index/$1";

How to check if Controller already exists when already configured route using Codeigniter?

On my website, I am loading the content dynamically from database like this
e.g mysite.com/about-us
for this, there is an enrtry in database, so it will load the content for 'about-us' & print it using "page" controller only.
for this what I have done is, I have added below configuration in routes.php
$route[':any'] = "page";
but lets say if I already have controller named "about-us" and I want to load that & not the one from database, how can I do that?
A smooth solution would be to use the error/missing_page controller and point it in the config/routes.php.
Then it would automaticly pick all existing controllers first, and then that controller.
You can also call show_404() if you don't find a record in the database.
This allows you to create new controllers without having to point all of them in the route file.
Read about 404 override here
you need to add this
$route['about-us'] = "aboutus";
$route['about-us/(:any)'] = "aboutus/$1";
before
$route[':any'] = "page";
as the CI route is not greedy, it will not check for the page controller after it finds the about-us controller.

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