How can i remove /index in URL in routing in cakePHP? - cakephp

I want to remove /index in URL.
I have already tried the following in routes.php file :
Router::connect('/Home/:index', array('controller' => 'Homes'));
and :
Router::connect('/Home/', array('controller' => 'Homes','action'=>'index'));

Try
Router::connect('/Home', array('controller' => 'Homes','action'=>'index'));

Use this code for create link
<?php
echo $this->Html->link(__('YOUR_TEXT'),array('controller' => 'Homes', 'action' => 'index',array('class' => "", 'id' => ""));
?>
If you are creating link with this code then if is there any change made in routes then cakephp will update link according to that

Related

how to link to a specific point on a page with cakephp

I'm trying to get a link in cakephp to point to a specific place on another page but what I imagined would work doesn't.
I'm using
<a name="Telstra"></a>
Telstra
Can anyone tell me the correct way?
You need to use the built-in link function of CakePHP. try to use this code.
<?php echo $this->Html->link('NameOfLink', array('controller' => 'ControllerName', 'action' => 'FunctionName/#Telstra')); ?>
See this url from cakephp docs:
http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::url
For creating link only
echo $this->Html->url(array(
"controller" => "posts",
"action" => "search",
"#" => "first"
));
and in your case for creating the link with anchor tag,
echo $this->Html->link('Telestra',array(
"controller" => "sponsors",
"action" => "index",
"#" => "Telstra"
));
If you want to load on a specific part of the page you must use ID on the element of your target rather than Name.
Example:
//Your target element on a page
<a name="Telstra"></a>
//URL that will redirect you to your target element.
Telstra
Or might as well code it the cakePhp style. :)
//URL
<?php echo $this->Html->link('Telstra', array('controller' => 'YourController', 'action' => 'YourFUnction', '#TargetElement'));
//Target Element
<a name="Telstra"></a>
Good Luck!
you need put a route in the folde of configuration /app/config/routes.php:
Router::connect('/index', array('controller' => 'yourController', 'action' => 'index'));
or
Router::connect('/index/*', array('controller' => 'yourController', 'action' => 'index'));
I think the parameter you're meaning to put is 'id' and not 'name' and I think you can just put #idName instead of the whole link.
For example:
<a id="Telstra"></a>
Telstra
For more information you can check here: http://www.w3schools.com/html/html_links.asp

CakePHP routing to controller and page

Is it possible to identify a controller and pagename in the same url
Router::connect('/:controller/');
Router::connect('/:pagename/', array('controller' => 'home','action'=>'index'));
www.example.com/controller
so that the controller goes to the :controller/index
www.example.com/pagename
so that the page goes to home/index
Its really confusing what really you wants. If a url ends with :controller like www.example.com/posts its generally map index action. Now if the url ends with pagename, means action, like www.example.com/mypage, you can map that as-
Router::connect('/mypage', array('controller' => 'homes', 'action' => 'index'));
So when a user browse www.example.com/mypage, it will map to HomesController and index action.
Try following code for pagename:
Router::connect('/:slug', array('controller' => 'home', 'action' => 'index'));
UPDATE 1
If you want to treat www.example.com/user as same as www.example.com/users then you need to add following, because CakePHP controller name is plural as per CakePHP naming convention:
Router::connect('/user/:action/*', array('controller' => 'users'));
UPDATE 2
Router::connect(':slug') overwrite all of default routing. So you need to use custom routing class as like as :
App::uses('SlugRoute', 'Routing/Route');
Router::connect(
'/:slug',
array('controller' => 'home', 'action' => 'index'),
array('routeClass' => 'SlugRoute')
);

cakephp url rewriting not working properly in the router file

I am facing some problem with rewriting cakephp urls
Here is my url:
domain.com/users/members/mygroup
domain.com/users/admins/mygroup
I want to rewrite this to
domain.com/mygroup/members
domain.com/mygroup/admins
I have tried the following code but it's not working
In the routes.php i have created the following routes
Router::connect('/:groupname/:members', array('controller' => 'users', 'action' => 'members'),array('pass' => array('groupname')));
Router::connect('/:groupname/:admins', array('controller' => 'users', 'action' => 'admins'),array('pass' => array('groupname')));
Here is the links:
<?php echo $this->html->link('Members',array('controller'=>'users','action'=>'members','groupname'=>$groupdata['Group']['group_slug'],'members'=>members),array('escape'=>false,'class'=>'links','id'=>'memlink'));?>
<?php echo $this->html->link('Admin',array('controller'=>'users','action'=>'admins','groupname'=>$groupdata['Group']['group_slug'],'admins'=>admins),array('escape'=>false,'class'=>'links','id'=>'admnlink'));?>
When i create routes like this the first routing i.e member routing is working fine, but the second routing admin is not working, it's picking the members action and executing the members method but the url appears correct, only the action is wrong.
How can i resolve this.
Try this.
Router::connect('/:groupname/:members', array('controller' => 'users', 'action' => 'members'),array('pass' => array('groupname', 'members')));
Router::connect('/:groupname/:admins', array('controller' => 'users', 'action' => 'admins'),array('pass' => array('groupname', 'admins')));
Update
Router::connect('/:groupname/:action', array('controller' => 'users'),array('pass' => array('groupname')));
Router::connect('/:groupname/:action', array('controller' => 'users'),array('pass' => array('groupname')));
working fine for me, put the code like this,
echo $this->html->link('Members',array(
'controller'=>'users','action'=>'members',
'groupname'=> 'mygroup'), array(
'escape'=>false,'class'=>'links','id'=>'memlink'
));
echo $this->html->link('Admin',array(
'controller'=>'users','action'=>'admins',
'groupname'=> 'mygroup'),array(
'escape'=>false,
'class'=>'links','id'=>'admnlink'
));

How to redirect in CakePHP with # in url, and not %23?

I am using CakePHP and I am doing this:
$this->redirect(array('controller' => 'users', 'action' => 'view',$id));
output in the browser: .../users/view/42
Since I am using JQueryUI tabs, I want the user to be redirected to the tab he just edited, so it should looks something like :
$this->redirect(array('controller' => 'users', 'action' => 'view',$id."#groups"));
output in the browser: .../users/view/42%23groups
But expected result: .../users/view/42#groups
Q: How to send a correct url with #id in it to send the focus ?
I want, if possible, not use a custom GET param that echo a js in the view.ctp to get the focus asked.
I hope it is a CakePHP issue and don't need to change some .htaccess like (How to rewrite a URL with %23 in it?)
Please read the documentation
http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::url
$this->redirect(array(
'controller' => 'users', 'action' => 'view', $id, '#' => 'groups'));
This is how I achieved it:
// Redirect to the home page.
$this->redirect(array(
'controller' => 'cars',
'action' => 'index',
'#' => 'thankyou'
));

Routing configuration in cakephp

I am trying to implement routing in cakephp. I want the urls to mapped like this...
www.example.com/nodes/main -> www.example.com/main
www.example.com/nodes/about -> www.example.com/about
So for this I wrote in my config/routes.php file..
Router::connect('/:action', array('controller' => 'nodes'));
Now, I got the thing going but when I click on the links, the url in browser appears like
www.example.com/nodes/main
www.example.com/nodes/about
Is there some way where I can get the urls to appear the way they are routed?
Setting in .htaccess or httpd.conf would be easy - but I don't have access to that.
Regards
Vikram
This should work:
Router::connect('/main', array('controller' => 'nodes', 'action' => 'main'));
Router::connect('/about', array('controller' => 'nodes', 'action' => 'about'));
You may also do something more powerful, like this:
$actions = array('main','about');
foreach ($actions as $action){
Router::connect('/$action', array('controller' => 'nodes', 'action' => '$action'));
}
Basically if your links are created with Html helper, with the following format:
<?php echo $this->Html->link('your link', array('controller'=>'nodes', 'action'=>'main'));?>
Then the Cake will convert the links properly to www.example.com/main
But if your links are
<?php echo $this->Html->link('your link', '/nodes/main/');?>
they will point to www.example.com/nodes/main

Resources