I want to change Codeigniter router
ex. cms_aboutus_cate
to
controller
--cms
--aboutus
--cate.php
is there any other efficient way to achieve using routing or any other way?
$route['^cms_aboutus_cate$'] = "cms/aboutus/cate/index";
Is important that you set the index method.
I recommended you HMVC if you want modulate your app.
You can set your url in config/routes.php file.
In your case, you can put:
$route['cms_aboutus_cate'] = 'cms/aboutus/cate';
So, the actual URL will be http://yourdomainname/cms/aboutus/cate but it will show in URL is this: http://yourdomainname/cms_aboutus_cate.
Reference:
https://www.codeigniter.com/userguide3/general/routing.html
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.
lets say my url is:
http://www.mysite.com/controller1/
I want to make routing so it wont look for the controller1, it will search for controller1 as the key.
so I will have a key like array for the controllers
<?php
$controllers_mapArr = array(
'controller1'=>'actual_controler_name',
);
?>
it will execute the value of the controller key 'actual_controler_name' but the URL will show controller1
any ideas?
You can easily do this using codeigniter's routing functionality, you have to add rules in application/config/routes.php file, following are the examples for doing
$route['journals'] = "blogs";
if your url is like http://example.com/journals then your URL will call blogs controller
$route['journals/joe'] = "blogs/users/34";
using this rule your if you access your url http://example.com/journals/joe then it will call blogs controller's users method with 34 as a value for function argument.
check the detailed document or routing here : http://ellislab.com/codeigniter/user-guide/general/routing.html
Ok found the answer with a littel help from my friend actually is one of the basics in codeigniter
any way there is the answer:
http://ellislab.com/codeigniter/user-guide/general/routing.html
I need to create a dynamic url in codeigniter like the facebook application. Is it possible to create such url using the codeigniter framework?
eg:
1. www.facebook.com/nisha
2. www.facebook.com/dev
You need to set up custom routing for the controller in application/config/routes.php. Like:
$route['([a-zA-Z]+)'] = "controller_name/function/$1";
This makes urls like the way you want, but it makes all of your controller inaccessible, that is because any '/controllername/parameter/' format will match with '(:any)' and will be redirected to our 'controller_name/function/'.
To stop controllers redirected by the CI router, you will have to explicitly define all of your controllers on the routes.php first then add the above mentioned routing rule at last line. Thats how i made it to work.
Hope that helps you in some way.
Its pretty easy to setup this by the use of routes. Read their routing guide
$route['([a-zA-Z]+)'] = "controller/user/$1";
However, if their is only one way of accessing the website, is like domain.com/username then its ok, otherwise, this will prove be a hard catch on the long run. On that case, limit the Route to a limited scope like
$route['users/([a-zA-Z]+)'] = "controller/user/$1";
This will help in the extending the system in numerous way
Try this way. it will reduce a lot of repetitive line if you have lots of controller but i don't know does it violate any CI rules.
//this code block should be placed after any kind of reserved routes config
$url_parts = explode('/',strtolower( $_SERVER['REQUEST_URI']) );
$reserved_routes = array("controller_1", "controller_2", "controller_3" );
if (!in_array($url_parts[1], $reserved_routes)) {
$route['([a-zA-Z0-9_-]+)'] = "controller_1/profile/$1";
}
How to use this things, the routes ?
For example, i my page is
controller/function
and i want the controller part and the function part to become
name
$route['controller/function'] =
"name";
and i tryed like that, but nothing happens, still when trying to go to controller/function it is controller/function not name
you need to do it the other way round.
actually you define a route to a desired controller function:
$route['en/name'] = 'en_controller/name_function';
also see here: CI Routing
You should read the manual URI Routing in CI URI Routing. There are some examples that will help you understand how to make your routing to work.
I'm hoping that this will be a simple question that someone can answer. I'm looking to build a CodeIgniter application that I can build pretty easily in regular PHP.
Example: I would like to go to http://locahost/gregavola and have rewritten using htaccess file to profile.php?user=gregavola. How can I do this in CodeIgniter?
Usually in htaccess I could write ^(\w+)$ profile.php?user=$1 but that won't work with the paths in CodeIgniter.
Any suggestions?
CodeIgniter turns off GET parameters by default; instead of rewriting the URL to a traditional GET style (IE, with the ?), you should create a user controller and send the request to:
http://localhost/user/info/gregavola
Then in the user controller, add the following stub:
function info($name)
{
echo $name;
}
From here you would probably want to create a view and pass $name into it:
$data['name'] = 'Your title';
$this->load->view('user_info', $data);
You can find all of this in the CodeIgniter User Guide, which is an excellent resource for getting started.
To map localhost/gregavola to a given controller and function, modify the routes file at application/config/routes.php like so:
$route['(:any)'] = "user/info/$1"
Routes are run in the order they are received, so if you have other routes like localhost/application/dosomething/, you will want to include those routes first so that every page in your entire app doesn't become a user page.
Read more about CI routes here: http://codeigniter.com/user_guide/general/routing.html
Good luck!