Cannot get rid of default controller in URL with multilingual codeigniter - php

When I land on my home page www.domain.com (with default controller 'home') the browser redirects to www.domain.com/en/home. What I would like to see is www.domain.com/en (google will see these pages as duplicate content I think?)
Is it possible to leave the default controller out of the URL so that only the language follows the domain i.e. www.domain.com/en?
Here is my code below:
$route['default_controller'] = "Home";
$route['404_override'] = '';
// '/en', '/es' URIs -> use default controller
$route['^(en|es)$'] = 'home'; //$route['default_controller']; //'home'
// route es translation of girls to girls
$route['es/chicas'] = "girls";
$route['es/chicas/chica/(:num)/(:any)'] = "girls/girl/$1/$2";
$route['es/chicas/etiquetas/(:num)/(:any)'] = "girls/tags/$1/$2";
// movies es routes
$route['es/peliculas'] = "movies";
$route['es/peliculas/pelicula/(:num)/(:any)'] = "movies/movie/$1/$2";
$route['es/fotos/galeria/pelicula/(:num)/(:any)'] = 'photos/gallery/movie/$1/$2';
$route['es/peliculas/etiquetas/(:num)/(:any)'] = "movies/tags/$1/$2";
$route['es/unirse'] = "join";
// general catch all for anything that doesn't fit rules above, but doesn't have a
// language prefix e.g. en/girls -> girls controller
$route['^(en|es)/(.+)$'] = "$2";

It's about your config/route.php file. Update your routes as you want exactly.

Related

CodeIgniter - Routing issues (four segments)

Following what I thought was an issue with $this->uri->segment(), I figured out that it is actually a routing issue. Problem is that I can't really figure out what is wrong, as it looks exactly like another route I am using, which works fine, except this one has two variable segments, rather than one (for the route that is working).
The file I am trying to show is located in:
[main folder]/application/views/tasks/calendar/calendar.php
And I can load it with the command:
$route['tasks/calendar'] = 'tasks/calendar';
However, when I want to pass the current year and month as the last two segments, it does not work:
$route['tasks/calendar/(:num)/(:num)'] = 'tasks/calendar/$1/$2';
To my knowledge this should mean that a link like this should work:
(...)/index.php/tasks/calendar/2015/03
However, it does not. The full routes.php looks like this:
$route['auth/(:any)'] = 'auth/view/$1';
$route['auth'] = 'auth';
$route['projects/delete/(:any)'] = 'projects/delete/$1';
$route['projects/create'] = 'projects/create';
$route['projects/(:any)'] = 'projects/view/$1';
$route['projects'] = 'projects';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
$route['category'] = 'category';
$route['category/(:any)'] = 'category/view/$1';
$route['tasks/create'] = 'tasks/create';
$route['tasks/calendar/(:num)/(:num)'] = 'tasks/calendar/$1/$2';
$route['tasks/calendar'] = 'tasks/calendar';
$route['tasks'] = 'tasks';
$route['tasks/(:any)'] = 'tasks/view/$1';
And my controller, tasks.php, looks like this:
public function calendar($year = null, $month = null) {
// Calender configuration (must be done prior to loading the library
$conf = array(
'start_day' => 'monday',
'show_next_prev' => true,
'next_prev_url' => base_url() . 'index.php/tasks/calendar'
);
// Load libraries and helpers
$this->load->library('calendar',$conf);
// Set variables for $data array
$data['year'] = $year;
$data['month'] = $month;
// Show page, including header and footer
$this->load->view('templates/header', $data);
$this->load->view('tasks/calendar', $data);
$this->load->view('templates/footer');
}
And the very simple view file, calendar.php, looks like this:
<?php
echo "Selected year: ".$year." and month: ".$month;
echo $this->calendar->generate($year, $month);
What the heck am I doing wrong? The delete routes for projects works just fine...
To build on what #Craig and #CodeGodie have just said, you need to re-order your route definitions slightly.
$route['tasks'] = 'tasks/index';
// Initial route that will use $year=null, $month=null
$route['tasks/calendar'] = 'tasks/calendar';
// This route will use whatever $year, $month the user provides
$route['tasks/calendar/(:num)/(:num)'] = 'tasks/calendar/$1/$2';
You may also want to set $year=null, $month=null to valid values.
$today = getdate();
if(is_null($year) || is_null($month)){
$year = $today['year'];
$month = $today['month']
}
The problem are your routes order. These routes:
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view'
should be at the very bottom of your routes list or else they will be seen prior your tasks routes.
Reference: Codeigniter User Guide - URI Routing
Note: Routes will run in the order they are defined. Higher routes
will always take precedence over lower ones.

Codeigniter i18n - Hide default_controller language Segment in URL

Everything with my i18n library is perfect except 1 little issue:
I want to make 1 home page to choose a language with links (lead to: en, fr, bg, ...):
Example: BG, EN
But always my default_uri is for an example: /bg and opens: www.mysite.com/bg
I want just to load plain URL up there as: www.mysite.com, load my START.PHP controller (no matter what name is, but not to be www.mysite.com/start) and after this to redirect with links to somewhere (bg/, en/, fr/)
Seems to be not so hard but don't know how to fix it
In MY_Lang.php:
// languages
var $languages = array(
'bg' => 'bulgarian',
'en' => 'english',
'fr' => 'french'
);
// special URIs (not localized)
var $special = array (
"admin", "start"
);
// where to redirect if no language in URI
var $default_uri = '';
In my routes.php:
$route['default_controller'] = "start";
$route['404_override'] = '';
// URI like '/en/about' -> use controller 'about'
//$route['(\w{2})/(.*)'] = '$2';
//$route['(\w{2})'] = $route['default_controller'];
$route['^(bulgarian|english|french)/(.+)$'] = "$2";
// '/en', '/de', '/fr' and '/nl' URIs -> use default controller
$route['^(bulgarian|english|french)$'] = $route['default_controller'];
I had a similar situation. For me the solution was that:
https://github.com/oleurud/Codeigniter_Multi-language_Package

How to remove action part in zend framework URLs

I`m using zend framework and my urls are like this :
http://target.net/reward/index/year/2012/month/11
the url shows that I'm in reward controller and in index action.The rest is my parameters.The problem is that I'm using index action in whole program and I want to remove that part from URL to make it sth like this :
http://target.net/reward/year/2012/month/11
But the year part is mistaken with action part.Is there any way ?!!!
Thanks in advance
Have a look at routes. With routes, you can redirect any URL-format to the controller/action you specify. For example, in a .ini config file, this will do what you want:
routes.myroute.route = "reward/year/:myyear/month/:mymonth"
routes.myroute.defaults.controller = reward
routes.myroute.defaults.action = index
routes.myroute.defaults.myyear = 2012
routes.myroute.defaults.mymonth = 11
routes.myroute.reqs.myyear = "\d+"
routes.myroute.reqs.mymonth = "\d+"
First you define the format the URL should match. Words starting with a colon : are variables. After that you define the defaults and any requirements on the parameters.
you can use controller_plugin to control url .
as you want create a plugin file (/library/plugins/controllers/myplugin.php).
then with preDispatch() method you can get requested url elements and then customize that for your controllers .
myplugin.php
class plugins_controllers_pages extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$int = 0;
$params = $this->getRequest()->getParams();
if($params['action'] != 'index' ) AND !$int)
{
$int++;
$request->setControllerName($params['controller']);
$request->setActionName('index');
$request->setParams(array('parameter' => $params['action']));
$this->postDispatch($request);
}
}
}

Default routing in Zend Framework after defining some routes

I've been writing a project since some time and I've used the default routing, the :module\:controller:\:action.
After some time, I've added some routers to my config like:
resources.router.routes.page.route = "page/:slug"
resources.router.routes.page.defaults.module = "default"
resources.router.routes.page.defaults.controller = "pages"
resources.router.routes.page.defaults.action = "view"
resources.router.routes.page.defaults.slug = ""
But, after that, when I click on some link generated by view URL helper with one of the new routes all other links ignore some of given paramters. Example, I've got route:
resources.router.routes.project.route = "project/:slug"
resources.router.routes.project.defaults.module = "projects"
resources.router.routes.project.defaults.controller = "projects"
resources.router.routes.project.defaults.action = "view"
resources.router.routes.project.defaults.slug = ""
If I go to a link /project/test then link like this:
$this->url(
array('module' => 'admin', 'action' => 'list-users', 'controller' => 'users')
, null,true
);
will point to "/project"
Is there any possibility to maintain the default routing on top of custom routes? Can I add some default router that will work the same as the default one? It's probably something simple but I maybe missed the point. Thanks for all the help.
I've added something like this but with no effect:
resources.router.routes.default.route = ":module/:controller/:action"
resources.router.routes.default.defaults.module = "default"
resources.router.routes.default.defaults.controller = "pages"
resources.router.routes.default.defaults.action = "view"
resources.router.routes.default.defaults.slug = ""
In order for you to set your custom routing, you need to get the router component and pass your routes into it.
This is how I did mine in a project I am working on. In your Bootstrap class, you create the following function
protected function _initRoutes()
{
$this->bootstrap('frontcontroller');
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
$myRoutes = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini','production');
$router->addConfig($myRoutes,'routes');
This calls the front controller and gets the router from it. I then pass my routes config into it.
I hope this answers your question.
The problem is that the options for url helper are as follows:
url($urlOptions, $name, $reset)
Therefore, when you set $name to null, current route ('project') is used. Not event setting $reset to true will help. Replace null with 'default' and it should work.

Zend Controller all actions to one action

I have categorie Controller, with index action
I need that when I enter address .com/categorie/education
it uses index action with parameter education .com/categorie/index/education
or redirect all actions to index ??
How are your routes setup? Given the lack of information provided I have to assume that you are using an .ini file, this is how it would work:
; CATEGORIE
categorie.type = "Zend_Controller_Router_Route_Static"
categorie.route = "categorie"
categorie.defaults.controller = categorie
categorie.defaults.action = index
categorie_view.route = "categorie/:slug"
categorie_view.defaults.controller = category
categorie_view.defaults.action = view
Note, I did change it up a bit, as the index action, imo, should list all categories. This way you have a viewAction to list the individual categorie.
To have it as you requested I believe it would be something like:
; CATEGORIE
categorie.route = "categorie/:slug"
categorie.defaults.controller = categorie
categorie.defaults.action = index

Categories