codeigniter Different/separate 404 page for frontend and backend - codeigniter

How can set auto route 404 page of frontend and backend different ? I have searched a long time but i have not found perfect answer.
Btw, can we make the same for error 500 ?

You can easily do it by using conditional statement.
go to application/config/routes.php and remove:
$route['404_override'] = '';
After that add the following code.
$req_uri = $_SERVER['REQUEST_URI']; // $req_uri = /myproject/backend
$req_uri = explode('/', $req_uri);
$req_uri = $req_uri[2]; // $req_uri[2] = backend
if($req_uri == 'backend'){
$route['404_override'] = 'Backend_error'; // Not found controller for backend
}else {
$route['404_override'] = 'Frontend_error'; // Not found controller for frontend
}
You can use echo statement to analyze further. and then do more stuff accordingly.

Related

Make user-friendly URL in Codeigniter with multi-language support

I have created a multi-language website.
Facing issue while creating SEO user-friendly URL.
current URL:
http://localhost/example/en/user/myaccount OR
http://localhost/example/fr/user/myaccount
want to change it with
http://localhost/example/en/myaccount OR
http://localhost/example/fr/user/myaccount
routes.php
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
// URI like '/en/about' -> use controller 'about'
$route['^(en|de|fr|nl|id|es)/(.+)$'] = "$2";
// '/en', '/de', '/fr','/es' and '/nl' URIs -> use default controller
$route['^(en|de|fr|nl|id|es)$'] = $route['default_controller'];
also tried with
$route["myaccount"] = "user/myaccount";
$route["^(en|de|fr|nl|id|es)$/myaccount"] = "user/myaccount";
Nothing will work in this case. Already used following in routes.php file other demo projects with out multi-language. There it's work perfectly.
$route["myaccount"] = "user/myaccount";
Thanks in advance
you can consult the documentation for routes here
https://www.codeigniter.com/user_guide/general/routing.html
and for your problem
$route["(:any)/myaccount"] = "user/myaccount/$1";

codeigniter CI-Smarty + Internationalization (i18n) for CodeIgniter 2.x

I'm developing a Codeigniter application that requires internationalization (i18n) support.
I'm using CI-Smarty (Smarty 3) as a template solution, and Internationalization (i18n) for CodeIgniter 2.x, but I cant get the multilingual support to work with CI Smarty, I've tried to change the code, tried adding routes, and doing both at the same time but without success.
My site controller:
public function index() {
$this->news();
} #index
public function news() {
$xml = $this->pages->news();
$data['news'] = $xml->xpath('//item');
$data['date'] = $xml->xpath('//item/pubDate');
$data['alphabet'] = $this->pages->alphabet();
$data['numbers'] = $this->pages->numbers();
$data['pagetitle'] = $this->config->item('news_pagetitle');
$data['description'] = $this->config->item('news_description');
$data['keywords'] = $this->config->item('news_keywords');
$this->pages->page('news', $data);
} #news
My pages model:
function page($page, $data = NULL) {
$data['base'] = base_url();
$data['current'] = current_url();
$data['page'] = $page;
$data['title'] = $this->config->item('title');
$data['tagline'] = $this->config->item('tagline');
$data['datetime'] = date('l, F d, Y');
$data['dateshow'] = date('Y-n-j');
$this->parser->parse('header', $data);
$this->parser->parse($page, $data);
$this->parser->parse('footer', $data);
} #page
My routes.php:
$route['^(en|pt)/(.+)$'] = "$2";
// '/en', '/de', '/fr' and '/nl' URIs -> use default controller
$route['^(en|pt)$'] = $route['default_controller'];
// Prettify the url
$route['default_controller'] = 'site';
$route['404_override'] = 'site/e404';
$route['scaffolding_trigger']= "";
$route['(:any)'] = 'site/$1';
$route['guide/(:num)'] = 'guide/$1';
$route['shows/(:any)'] = 'shows/$1';
$route['shows/(:any)/(:num)'] = 'shows/$1/cat/$2';
$route['title/(:num)/(:any)'] = 'title/$1';
When I request the index page using the www.example.com/en/ which will show the news info it loads fine, but if I request the page news using www.example.com/en/news it does not load the news, and instead loads the 404 page, and do the same to all other pages.
The application works fine without the internationalization (i18n) support, loading all the pages ok, including the news page.
And also the internationalization (i18n) support works fine, I've tested it in a version without the CI-Smarty.
I need to translate this app into multiple languages, so I need to find a way for Codeigniter with CI-Smarty to support it. Is there one? Thanks to all those how can help.

Codeigniter pagination routing issue

I have configured a route in routes.php as,
$route['job-history/(:num)'] = "customer/customer/jobhistory/$1";
and pagination configuration in controller as follows,
$this->load->library('pagination');
$config['per_page'] = 25;
$config['total_rows'] = 100;
$config['base_url'] = $this->config->item('base_url').'job-history';
$config["uri_segment"] = 2;
$this->pagination->initialize($config);
$page = ($this->uri->segment(2)) ? $this->uri->segment(3) : 0;
Its showing 404 error page while loading,
www.example.com/job-history
It will work if manually add a zero like www.example.com/job-history/0.
How can I load www.example.com/job-history as first page. Whats wrong in my confuration. Any help please
You'll need a route for the single job-history segment as well.
$route['job-history/(:num)'] = 'customer/customer/jobhistory/$1';
$route['job-history'] = 'customer/customer/jobhistory';
Since, in route.php, you have only mentioned for job-history pages that has a number segment after it and there is no such rule for your page job-history only, it gets redirected to 404.
Add
$route['job-history'] = 'customer/customer/jobhistory';
before
$route['job-history/(:num)'] = 'customer/customer/jobhistory/$1';

CodeIgniter router doesn't work on a specific server

I am developing a single page web site so all routes should lead to the default router. This is how I've achieved that through the routes.php file:
$route['default_controller'] = "home";
$route['404_override'] = '';
$route['admin'] = 'admin/home';
$route['^/'] = "home";
$route['(.*)'] = $route['default_controller'];
Thats working as expected on a test server and on the local host, but on the actuall server, when I try to find any of the pages I am getting the 404 error. Any ideas what could be wrong?
$route['default_controller'] = "home";
$route['404_override'] = "home/index";
$route['.*'] = "home/index/$1"

CodeIgniter: Understanding routes

I have the this route defined in routes.php
$route['user'] = "user_controller";. The user controller has a method logout() but when I try this URI user/logout I get a 404. In the same way when I use this URI user/index I get a 404.
routes.php
// custom routes
$route['start'] = "start_controller";
$route['register'] = "register_controller";
$route['user'] = "user_controller";
// other routes
$route['default_controller'] = "start_controller";
$route['404_override'] = '';
According to CI
Note: Routes will run in the order they are defined. Higher routes
will always take precedence over lower ones.
$route['default_controller'] and $route['404_override'] must always be on top above others
$route['user/logout'] = "user_controller/logout";
$route['user/index'] = "user_controller";
Example i will type a user/logout then it will proceed to user_controller/logout you have to define the URL you would like to redirect
Yep, you have to specify a route for each particular method.
Here's an example from my routes.php :
/* User Authentication Controller */
$route['login'] = "auth/login";
$route['logout'] = "auth/logout";
$route['register'] = "auth/register";
$route['forgot'] = "auth/forgot";

Resources