CodeIgniter - Routing issues (four segments) - php

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.

Related

CodeIgniter - how to remove class name in url route

I have http://localhost/CIrbbps/index.php/class_name/method_name . how to remove class name?, so i can get just http://localhost/CIrbbps/index.php/method_name . thank you
In order to map www.domain.com/services to pages/services you would go like:
$route['services'] = 'pages/services'
If you want to map www.domain.com/whatever to pages/whatever and whatever has a few variants and you have a few controllers, then you would do :
// create rules above this one for all the controllers.
$route['(:any)'] = 'pages/$1'
That is, you need to create rules for all your controllers/actions and the last one should be a catch-all rule, as pointed above.
If you have too many controllers and you want to tackle this particular route, the in your routes.php file it is safe to:
$path = trim($_SERVER['PATH_INFO'], '/');
$toMap = array('services', 'something');
foreach ($toMap as $map) {
if (strpos($path, $map) === 0) {
$route[$map] = 'pages/'.$map;
}
}
Note, instead of $_SERVER['PATH_INFO'] you might want to try $_SERVER['ORIG_PATH_INFO'] or whatever component that gives you the full url path.
Also, the above is not tested, it's just an example to get you started.
CodeIgniter Routes - remove a classname from URL for one class only
try this:
$route['(:any)'] = "account/$1";

CodeIgniter - $this->uri->segment() returns empty

I am trying to get the CodeIgniter calender library to work, and for that I need to use $this->uri->segment(), according to the documentation at least. The url for the calender looks like this:
http://www.example.com/index.php/tasks/calendar/2015/03
Which means, as far as I can understand, that $this->uri->segment(3) would be the year, and $this->uri->segment(4) the month. However, both of these returns nothing at all, here is the code for my calender page:
echo "Selected year: ".$this->uri->segment(3)." and month: ".$this->uri->segment(4);
echo $this->calendar->generate($this->uri->segment(3), $this->uri->segment(4));
The first line is merely used for debugging purposes, and confirms that none of the commands returns any output. The calender library is loaded in the controller, with the following code:
public function calendar() {
// 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);
// Show page, including header and footer
$this->load->view('templates/header', $data);
$this->load->view('tasks/calendar', $data);
$this->load->view('templates/footer');
}
Can anyone tell me where I am failing? I read the documentation over and over again, and checked my code, but I can't see what I do different than their example?
UPDATE
I think I have ruled out that the problem is related to the $this->uri->section() function. Is likely to do with the controller loading an incorrect page, when additional sections is added to the URL after .../tasks/calendar/(...). I still haven't found a solution, but I wanted to let you know, in case others who read this had the same issue.
UPDATE 2
I think it might be a routing issue, the routes.php looks like this, for anything related to tasks:
$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';
for this to work you have to load the Codeigniter url helper since youre using base_url(). You can do this by running this $this->load->helper('url'); or by going to your autoload.php and placing it there in helper
Ok the problem here is not your URI class. The problem is that youre trying to use $this->uri->segment() inside your view. This is not how you should do this. You should only use that inside your controller. To fix this, you have two options, you can either pass your year and month as parameters to your controller method or you can use the uri->segment inside your controller.
Example 1: Passing year and month as parameters
URL: index.php/tasks/calendar/2015/03
Controller:
class Tasks extends MY_Controller {
public function calendar($year, $month){
$conf = array(
'start_day' => 'monday',
'show_next_prev' => true,
'next_prev_url' => base_url() . 'index.php/tasks/calendar'
);
$this->load->library('calendar',$conf);
$data = array(
"year" => $year,
"month" => $month
);
$this->load->view('tasks/calendar', $data);
}
}
View:
echo "Selected year: $year and month: $month";
echo $this->calendar->generate($year, $month);
Example 2: getting the variable using $this->uri
URL: index.php/tasks/calendar/2015/03
Controller:
class Tasks extends MY_Controller {
public function calendar(){
$conf = array(
'start_day' => 'monday',
'show_next_prev' => true,
'next_prev_url' => base_url() . 'index.php/tasks/calendar'
);
$this->load->library('calendar',$conf);
$data = array(
"year" => $this->uri->segment(3),
"month" => $this->uri->segment(4)
);
$this->load->view('tasks/calendar', $data);
}
}
View:
echo "Selected year: $year and month: $month";
echo $this->calendar->generate($year, $month);
What do you get if you try echo
"Selected year: ".$this->uri->segment(1)." and month: ".$this->uri->segment(2);
I'm pretty sure the index is relative based on the default_controller path setting in the routes.php config.

Issue with Codeigniter Pagination And Search

I am trying to give facility of pagination and search keyword in one report with Code-igniter Framework.
Below is the Controller code for that Purpose:
$queryString="?start_date=".$values['start_date']."&end_date=".$values['start_date']."&keyword=".$values['keyword']."";
if($this->uri->segment(3)){
$offset=$this->uri->segment(3);
$config['base_url'] = base_url()."reports/mysearch_search/".$offset.$queryString;
}else{
$config['base_url'] = base_url()."reports/mysearch_search/".$queryString;
}
$query = $this->db->query("SELECT * FROM (`dlrReport`) WHERE LEFT(`res_submit_date`,10) >= '".$values['start_date']."' AND LEFT(`res_submit_date`,10) <= '".$values['end_date']."' AND (number LIKE '%".$values['keyword']."%' OR source LIKE '%".$values['keyword']."%')");
$config['total_rows']=$query->num_rows();
$config['per_page'] = 2;
$this->pagination->initialize($config);
$where = array('LEFT(res_submit_date,10) >=' => $values['start_date'],'LEFT(res_submit_date,10) <=' =>$values['end_date']);
$this->db->where($where);
$this->db->where("(number LIKE '%".$values['keyword']."%' OR source LIKE '%".$values['keyword']."%')");
$res = $this->db->get('dlrReport', $config['per_page'], $this->uri->segment(3));
$dlr['details']= $res->result();
$dlr['start_date']=$values['start_date'];
$dlr['end_date']=$values['end_date'];
$dlr['keyword']=$values['keyword'];
$this->load->view('mysearch',$dlr);
Getting Below link in pagination links which is not working:
http://Host/project/reports/mysearch_search/?start_date=2014-01-23&end_date=2014-01-23&keyword=/2
Url i am expecting in pagination links is like:
http://Host/project/reports/mysearch_search/2?start_date=2014-01-23&end_date=2014-01-23&keyword=
How can i will Get right Url for searching Purpose?
Here is nice example from codeignter itself. Codeigniter Pagination check this.
By default codeigniter is expecting the pagination url like this.
http://localhost/project/reports/mysearch_search/2014-01-23/2014-01-23/2
$start_date =$this->uri->segment(5);
$end_date = $this->uri->segment(6);
$keyword = $this->uri->segment(7);
and if you need this kind of url then you have to change the
http://example.com/index.php?c=test&m=page&per_page=20
then you have to change to set this on config file
$config['enable_query_strings'] set to TRUE

Cannot get rid of default controller in URL with multilingual codeigniter

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.

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);
}
}
}

Categories