Dynamic routes in codeigniter - codeigniter-2

How to write custom routes in codeigniter?
$route['codeigniter/getAllCategories'] = "codeigniter/category_controller/getAllCategories";
$route['default_controller'] = "admin";
$route['404_override'] = '';
http://localhost/codeigniter/category_controller/getAllCategories
$route['(:any)/(:any)'] = 'category_controller/$1/$2';

Instead of what you are doing , you can do the concept of slug in codeigniter to create dynamic routes. Check this link.
http://osvaldas.info/smart-database-driven-routing-in-codeigniter

Related

codeigniter redirecting to a url

I have a website with a url like so (which works fine)
http://www.page.com/en/controller
But there always have to be a language and a controller in the url otherwise the page doesn't load or there is no language (no text).
Is it possible that when I enter a url like this
http://www.page.com
I get redirected to
http://www.page.com/en/controller
And the controller would be hidden? Only this would be left (my links require first segment to load a page with a particular language)
http://www.page.com/en
p.s tried routing and redirect, but with no luck
Sorry for the late response, I was away.
my routes
$route['default_controller'] = 'arena/display';
$route['(:any)/renginiai'] = "renginiai/getevents";
$route['(:any)/arena'] = "arena/display";
$route['(:any)/paslaugos'] = "paslaugos/displayServices";
$route['(:any)/kontaktai'] = "kontaktai/displayContacts";
$route['(:any)'] = 'pages/view/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
My_Controller which is located in core folder. I load my language libraries from here.
/**
*
*/
class MY_Controller extends CI_Controller{
public function __construct()
{
parent::__construct();
$languages = array("lt", "en");
if(in_array($this->uri->segment(1), $languages)){
$this->lang->load($this->uri->segment(1), $this->uri->segment(1));
}
}
}
And this is my front page controller
<?php
class Arena extends MY_Controller{
public function display($year = NULL, $month = NULL){
/*$this->load->model('Mycal_model');*/
$this->load->model('Image_model');
$data['images'] = $this->Image_model->get_image_data();
$this->load->view('includes/head');
$this->load->view('includes/nav', $data);
$this->load->view('includes/header', $data);
//$this->load->view('includes/calendar', $data);
$this->load->view('includes/section');
$this->load->view('includes/footer');
}
}
open application/config/routes.php file and change
$route['default_controller'] = "en/controller";

Customize dynamic URL.change ?id to name

I'm using codeigniter for make dynamic website. In news page, I use url method like: http://test.com/test/test/news_details?id=27
how can i change my url to put news title within ?id=27
example:
http://test.com/test/test/news_details/pass-this-url
"pass-this-url" refer to id=27.
Best Regards.
Use routes : http://www.codeigniter.com/user_guide/general/routing.html
In your case it will be something like this :
$route['pass-this-url'] = "test/test/news_details?id=27";
So http://www.example.com/pass-this-url will be understand by codeigniter as http://www.example.com/test/test/news_details?id=27
however, if you want to make it dynamic, you will have to call your db :
require_once( BASEPATH .'database/DB'. EXT );
$db =& DB();
$query = $db->get( 'news' );
$result = $query->result();
foreach( $result as $row )
{
$route[$row->title] = "test/test/news_details?id=". $row->id;
//We suppose here that your title is URL friendly.
}
Go to application/config/config.php and check this:
$config['enable_query_strings'] = FALSE;

Remove Segment From URL Codeigniter

Is it possible to remove a segment from URL but still redirecting it to location. I use a MY_Controller for all the controllers.
Example: localhost/project/maintenance
Would become localhost/project
Router.php
$route['default_controller'] = "catalog/common/home/index"; // Always Default Controller
$route['404_override'] = '';
$route['maintenance'] = "catalog/common/maintenance"; // Maintenance Controller
I tried this below know luck
if($this->config->item('system_maintenance') == FALSE) {
$route['default_controller'] = "catalog/common/home/index";
} else {
$route['default_controller'] = "catalog/common/maintenance/index";
}
I Think I got it working now in routes works fine now so far.
if($this->config->item('system_maintenance') == TRUE) {
$route['default_controller'] = "catalog/common/maintenance/index";
} elseif ($this->config->item('system_maintenance') == FALSE) {
$route['default_controller'] = "catalog/common/home/index";
}

CodeIgniter routes and pagination adding “/page/” to all links

I’ve implemented pagination like the following:
$this->load->library('pagination');
$perpage=10;
$config['base_url'] = site_url().'news/page';
$config['total_rows'] = $this->news_model->getnews(array('count' => true));
$config['per_page'] = $perpage;
$config['uri_segment'] = 3;
$config['num_links'] = 8;
$news = $this->news_model->getnews(array('limit' => $perpage,'offset'=>$offset));
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$data['news'] = $news;
$data['page'] = "news";
$this->load->view('index', $data);
I’m also using the following routes:
$route["news"] = "news/news_list";
$route["news/page"] = "news/news_list";
$route["news/page/(:num)"] = "news/news_list/$1";
$route["news/detail/(:any)"] = "news/news_detail/$1";
The problem that I’m facing is that although the pagination is working fine when i go to the second page or any other page after clicking on the pagination links - all of my other links on the page get the /page/ added in front of them like -> /page/detail/aaaaaa so that my route $route["news/detail/(:any)"] = "news/news_detail/$1"; can not identify it as the detail link.
Why is the /page/ added to all of the links? Do i need any routes for Pagination?
Your $config['base_url'] is news/page, that’s why /page is added to all your links.
I don’t think you need these routes for pagination, but if you want them, you should use these routes in $config['base_url'].
$route["news/page/(:num)"] = "news/news_list/$2";
$route["news/detail/(:any)"] = "news/news_detail/$1";

Codeigniter Routing Settings for Tank Auth

I'm using Tank-Auth for my application. And the only problem I have is activating and resetting passwords for accounts.
For login, register, logout; I have no problem with this codes;
$route['login'] = "/auth/login";
$route['logout'] = "/auth/logout";
$route['register'] = "/auth/register";
But for activating accounts and resetting passwords, those codes are not working;
$route['activate/:num/:any'] = "/auth/activate/$1/$2";
$route['reset_password/:num/:any'] = "/auth/reset_password/$1/$2";
PS: The first segment after 'activate' is 'user id' and second segment is key like this: example.com/activate/2/4784322e48916efec1153c53d25453c7
The solution is changing url segments in the (auth) controller from this:
$user_id = $this->uri->segment(3);
$new_pass_key = $this->uri->segment(4);
to this:
$user_id = $this->uri->segment(2);
$new_pass_key = $this->uri->segment(3);
After this change, the routing for activate&reset_password is working with those rules
$route['activate/:num/:any'] = "/auth/activate/$1/$2";
$route['reset_password/:num/:any'] = "/auth/reset_password/$1/$2";

Resources