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';
Related
I'm building an online store based on CodeIgniter. I'd like URLs to look like this? What is the solution for this type of SEO friendly url.
http://example.com/[product-category]/[product-sub category]
I need this url:
example.com/women/sarees-sari
But my url is generated
example.com/Product/item/MQ==/women/sarees-sari
/Product/ is my controller,
/item/ is function name,
/MQ==/ is my product id
You can use routing to handle your request url. It's simple. For example for your case:
$route['women/sarees-sari'] = 'Product/item/MQ==';
Codeigniter has _remap function that can be called on controllers. So you can call this on core controller or main controller, and call your function that wish.
CodeIgniter has very good routing System so you can modify your url as per your requirement and linking using /application/config/routes.php file.
If you open this file first time, you will see only default controller, i.e $route['default_controller'] = 'welcome';
but you can add as many routes as you want. Like in your case for seo, you should add
$route['women-sarees-sari'] = 'Product/item/MQ=='; and this will route the user from www.example.com/women-sarees-sari to correct controller and method.
I'm trying to make my CodeIgniter application work similarly to WordPress.
I want to be able to make these kind of URLs:
http://www.example.com/my-post-example
http://www.example.com/new-headline-here
http://www.example.com/i-love-stackoverflow
My routing:
$route['(:any)'] = "core/index/$1";
Which will call my Core controller and pass the page name into the index function.
I then lookup in my database for the page name and display the page to the user. So far so good.
However, there will be times when I want to call another controller. For example:
http://www.example.com/admin/edit_page/3
http://www.example.com/admin/settings
Now I assume my route will just grab all these rules and send them into my Core controller. Is there a way to make an exception for certain pages? Or is it a good idea to do this check inside my Core controller.
For example,
if ($page not in DB) {
// Call controller/method
}
This seems a little redundant since I just want CodeIgniter to handle this.
The routing rule you using it is OK for your purpose.
If you use http://www.example.com/admin/edit_page/3 this link it will send you admin controller and edit_page method.It will not use routes any rule.
However you will get one problem if your link looks like this
http://www.example.com/my-post-example/test
It will try to go my-post-example controller and test method.
Again http://www.example.com/admin will use routes any rule, means it will redirect your to core controller instead of admin/index. In that case your url should be http://www.example.com/admin/index
Finally If you call your other link with controller/method name it will be OK using your any rule
I am building a cms in codeigniter and i want to remove controller name and function name form the url and just print the alias.
I will be having two controllers one for static pages and other for blog posts with categories.
Please help, Suggestions reagrding modification of two controllers are also welcome.
You will need to override the default 404 controller in application/config/routes.php.
$route['404_override'] = 'content';
Any request that can't be mapped to a controller will be passed to the application/controllers/content.php controller
Your Content controller, or whatever you decide to call it, will parse the uri [$this->uri->segment(1)] and check for a matching reference in your CMS database.
If there is no match in the database, then you can look for a static view in the views folder and load it.
if(is_file(FCPATH.'views/'.$this->uri->segment(1).'.php')) {
$this->load->view($controller,$this->data);
}
If no static view is found, and there is no matching content in the db, call the show_404() function.
Using this method, you will keep the default CI functionality of uri mapping, so at any time, you can add controllers as you normally would and the app will perform like a vanilla CI install.
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
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.