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";
Related
I am working on a project based on cakephp. I just wanted to know How can I make a perfect URL redirection form database value.
For example
I give here two current URLs and desired URLs
1.Current
/search?vendor=combo-training-certification-courses
1.Desired
combo-training-certification-courses
2.Current
/search?vendor=pmi-training-certification-courses
2.Desired
/pmi-training-certification-courses
Please tell me how can I achieve it...
Just Add following code in config/routes.php
App::uses('ClassRegistry', 'Utility');
$Route = ClassRegistry::init('Vendor'); //MODEL NAME You can change it as your needs.
$routes = $Route->find('all');
foreach ($routes as $route) {
Router::connect('/', array('controller' => $route['Route']['controller'], 'action' => $route['Route']['action']));
}
Reference
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.
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);
}
}
}
I have a route as:
((?<directory>\w+)/?)?((?<controller>\w+)/?)?((?<action>\w+)/?)?((?<id>\d+))?
It works fine but it causes my system to have to include the default controller (index) for all routes to the sub routes. For example, if my page URI is /blog/post (where blog is the directory and post would be the action), my actual URI would have to be blog/index/post - I'd like to be able to fall back to just using blog/post instead.
So, I would like it to be routed to:
directory = blog
controller = index
action = post
Obviously this causes issues when the second parameter is actually a controller. For example directory/controller/action would be routed incorrectly.
Is there a routing method to detect that there are three word parameters, possibly followed by a numeric parameter, which can do what I need?
For claification:
param/param/param(?/id) would be: directory/controller/action(/id)
param/param(?/id) would be: directory/default_controller/action(/id)
i'd actually think that you want to alias blog/index/post with blog/post; insert it as a route before the "catch-all" route that you have; the "one big shoe fits all" approach is not always the best. Especially, if you only have 1 such particular use case.
edit:
"kohana's routing system" is daunting; can't make sense of the elephant they're trying to give birth to there... here are some other suggestions:
Take this issue to the manufacturer; this is definetely an FAQ question
Mess around with the regex patterns. Here's a snippet that might be useful (i put it inside a PHP test case, but you could easily decouple it)
public function testRoutePatterns(){
$data = array(
array(
//most specific: word/word/word/id
'~^(?P<directory>\w+)/(?P<controller>\w+)/(?P<action>\w+)/(?P<id>.*)$~i',
'myModule/blog/post/some-id',
array('directory'=>'myModule', 'controller'=>'blog', 'action'=>'post', 'id'=>'some-id'),
true
),
array(
//less specific: word/word/id
'~^(?P<directory>\w+)/(?P<action>\w+)/(?P<id>.*)$~i',
'blog/post/some-id',
array('directory'=>'blog', 'action'=>'post'), //need to inject "index" controller via "defaults()" here i guess
true
),
);
foreach ($data as $d) {
$matches = array();
list($pattern, $subject, $expected, $bool) = $d;
$actual = (bool) preg_match($pattern, $subject, $matches);
$this->assertEquals($bool, $actual); //assert matching
$this->assertEquals(array(), array_diff($expected, $matches)); //$expected contained in $matches
}
}
As explained on this answer, if you have some route like this:
Route::set('route_name', 'directory/controller/action')
->defaults(array(
'directory' => 'biz',
'controller' => 'foo',
'action' => 'bar',
));
You should have the directory structure like this:
/application/classes/controller/biz/foo.php
I have a mobile page running on a subdomain "m.mydomain.com". This is all working fine, but I would like to remove the controller in the URL when using the subdomain.
m.mydomain.com/mobiles/tips
should become
m.mydomain.com/tips
by using the HTML-Helper.
At the moment a link looks like that:
$html->link('MyLink', array('controller' => 'mobiles', 'action'=> 'tips'));
I tried several possible solutions with the routes and also some hacks in the bootstrap but it did not work out for me.
In the CakeBakery I found this but that does not solve my issue.
Does anyone have an idea for this issue?
Gathering code from the page you mentioned:
Constraint: you cannot have a controller called tips or foo in this setup
In /config/routes.php:
$subdomain = substr( env("HTTP_HOST"), 0, strpos(env("HTTP_HOST"), ".") );
if( strlen($subdomain)>0 && $subdomain != "m" ) {
Router::connect('/tips',array('controller'=>'mobiles','action'=>'tips'));
Router::connect('/foo', array('controller'=>'mobiles','action'=>'foo'));
Configure::write('Site.type', 'mobile');
}
/* The following is available via default routes '/{:controller}/{:action}'*/
// Router::connect('/mobiles/tips',
// array('controller' => 'mobiles', 'action'=>'tips'));
// Router::connect('/mobiles/foo',
// array('controller' => 'mobiles', 'action'=>'foo'));
In your Controller action:
$site_is_mobile = Configure::read('Site.type') ?: '';
Then in your view:
<?php
if ( $site_is_mobile ) {
// $html will take care of the 'm.example.com' part
$html->link('Cool Tips', '/tips');
$html->link('Hot Foo', '/foo');
} else {
// $html will just output 'www.example.com' in this case
$html->link('Cool Tips', '/mobiles/tips');
$html->link('Hot Foo', '/mobiles/foo');
}
?>
This will allow you to output the right links in your views (in a bit I'll show you how to write even less code) but the $html helper will not be able -- by no amount of magic -- to use controller-action routes to another domain. Be aware that m.example.com and www.example.com are different domains as far as the $html helper is concerned.
Now, if you want you can do the following in your controller to take some logic off your view:
<?php
$site_is_mobile = Configure::read('Site.type') ?: '';
if ( $site_is_mobile !== '' ) {
$tips_url = '/tips';
$foo_url = '/foo';
} else {
$tips_url = '/mobile/tips';
$foo_url = '/mobile/foo';
}
// make "urls" available to the View
$this->set($tips_url);
$this->set($foo_url);
?>
And in your view you don't need to worry about checking whether the site is being accessed via m.example.com/tips or www.example.com/mobile/tips:
<?php echo $html->link("Get some kewl tips", $tips_url); ?>
For more advanced routing in CakePHP-1.3 refer to Mark Story's article on custom Route classes
Let us know ;)