Codeigniter minimize URLs with extending controller and multilingual - codeigniter

I've configured Codeigniter 2.1 with i18n and extended controller.
I've hide the main controller "main" and I kept visible "admin" and "blog" controller.
This urls works fine:
www.mysite.com/ en / functionname
www.mysite.com/ en / blog /
This is my problem:
www.mysite.com/ it / blog / functionname
With the main controller "blog" everything after "/" is ignored.
Is it possible to do this?
My routes.php file:
$default_controller = "main";
$language_alias = array('it','en');
// exceptions
$controller_exceptions = array('admin','blog');
// route
$route['default_controller'] = $default_controller;
$route["^(".implode('|', $language_alias).")/(".implode('|', $controller_exceptions).")(.*)"] = '$2';
$route["^(".implode('|', $language_alias).")?/(.*)"] = $default_controller.'/$2';
$route["^((?!\b".implode('\b|\b', $controller_exceptions)."\b).*)$"] = $default_controller.'/$1';
foreach($language_alias as $language) {
$route[$language] = $default_controller.'/index';
}
$route['404_override'] = '';
// URI like '/en/about' -> use controller 'about'
$route['^(it|en)/(.+)$'] = "$2";
// '/it', '/en' URIs -> use default controller
$route['^(it|en)$'] = $route['default_controller'];
If I remove lang in my URL everything works fine:
www.mysite.com/ blog / functionname

I think you need another segment in your routes for accessing the controllers function.
so you'll need a second line:
// '/it', '/en' URIs -> use default controller
$route['^(it|en)$'] = $route['default_controller'];
// URI like '/en/about' -> use controller 'about'
$route['^(it|en)/(.+)$'] = "$2";
// URI like '/en/about/test' -> use controller 'about' with function 'test'
$route['^(it|en)/(.+)/(.+)$'] = "$2/$3";
I don't use the i18n or support multiple language in any project atm so I can't test but this should do the trick.

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: 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";

Overriding routes in codeigniter [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
CodeIgniter - When using $route[‘(:any)’] = ‘pages/view/$1’ how to use other controllers?
I'm working on a url shortener/redirect web app in codeigniter and have ran into an issue with my routes.
The first route i have is:
$route['(:any)'] = "redirect/index/$1";
This is what handles the redirect, so example.com/dwB would goto the redirect controller.
Underneath I have a few routes that override that for pages and other controllers etc. They work fine when i describe the entire url including arguments etc but I have now ran into trouble as i have some dynamic urls like:
example.com/stats/view/dwB
Or facebook auth responses etc, which i can't obviously write a route for. I've tried using something like:
$route['stats/view/(:any)'] = "stats/view/$1";
but none of these appear to override the first route. Here is my entire routes.php document.
$route['(:any)'] = "redirect/index/$1";
$route['shorten/create'] = "shorten/create"; // overwrite the previous route
$route['stats/view/(:any)'] = "stats/view/$1"; // allow the stats controller to be used
$route['login'] = "auth/login"; // allow the login(auth) controller to be used
$route['register'] = "auth/register"; // allow the login(auth) controller to be used
$route['auth_social/fblogin'] = "auth_social/fblogin"; // allow the login(auth) controller to be used
$route['dashboard'] = "dashboard"; // allow the login(auth) controller to be used
$route['auth/logout'] = "auth/logout"; // allow the login(auth) controller to be used
$route['auth'] = "auth/index"; // allow the login(auth) controller to be used
$route['default_controller'] = "pages";
$route['404_override'] = '404';
As I mentioned in the comments, place your "any" route at the end of your custom routes, like this:
$route['shorten/create'] = "shorten/create"; // overwrite the previous route
$route['stats/view/(:any)'] = "stats/view/$1"; // allow the stats controller to be used
$route['login'] = "auth/login"; // allow the login(auth) controller to be used
$route['register'] = "auth/register"; // allow the login(auth) controller to be used
$route['auth_social/fblogin'] = "auth_social/fblogin"; // allow the login(auth) controller to be used
$route['dashboard'] = "dashboard"; // allow the login(auth) controller to be used
$route['auth/logout'] = "auth/logout"; // allow the login(auth) controller to be used
$route['auth'] = "auth/index"; // allow the login(auth) controller to be used
// Move "any" route down here...
$route['(:any)'] = "redirect/index/$1";

How to set url for particular controller action - Magento

If I have module mymodule in which I have index Controller. In which I have subaction as 'subaction'
Normally I access page as
http://www.mywebsite/index.php/mymodule/index/subaction
How can I set url from code such as
http://www.mywebsite/index.php/subaction
or
http://www.mywebsite/index.php/mymodule/subaction
Note :: I do not want to create new controller I want this in the same index controller.
Magento URL-to-controller matching works through the Standard router which expects URLs to have a specific form. If you want to change that you have a few options at your disposal:
Deprecated config-based URL rewrites
Create URL rewrite entries in core_url_rewrite table
Create a custom router class to match the URL patterns which you would like to use
When considering how URL matching should work, you need to consider how Magento will expect to build the URL using its native URL calculation tools as well as how to get requests to match.
You can do this by using routes
in your bootstrap do the following
protected function _initMyRoutes() {
$this->bootstrap('frontController');
$front = $this->getResource('frontController');
$router = $front->getRouter();
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini', APPLICATION_ENV);
$router->addDefaultRoutes();
$router->addConfig($config, 'routes');
return $router;
}
and in the configs directory create a file called routes.ini and in it place the following
routes.myRoute.type = "Zend_Controller_Router_Route_Static"
routes.myRoute.route = "/subaction/"
routes.myRoute.defaults.module = "mymodule"
routes.myRoute.defaults.controller = "index"
routes.myRoute.defaults.action = "subaction"
OR
you can add the route directly in your bootstrap with
protected function _initMyRoutes() {
$this->bootstrap('frontController');
$front = $this->getResource('frontController');
$router = $front->getRouter();
$router->addDefaultRoutes();
$route = new Zend_Controller_Router_Route_Static(
'subaction',
array('module' => 'mymodule', 'controller' => 'index', 'action' => 'subaction')
);
$router->addRoute('subaction', $route);
return $router;
}
That should do the trick but be adviced using routes can really be a pain.
More about routes in the ZF manual

Resources