Cakephp URL masking? - cakephp

For example: I have a controller: "Services" and an action called "web". Thus, my url would be:
http://www.domain.com/services/web/
How do I mask the url, such that if I type:
http://www.domain.com/servicesweb
will display exactly as http://www.domain.com/services/web/
I am reading the htaccess, not sure if its a correct solution to this.

It appears you missed the entire chapter in the CakePHP docs about Routing, which is this 'url masking' you speak of.
In your /app/config/routes.php file you will need to add this line:
Router::connect('/servicesweb', array('controller' => 'services', 'action' => 'web'));
Be sure to read the book for clarification on routing.

Related

CakePHP URLs having .html extension

We are converting an exisiting HTML site into a CMS using CakePHP. Since SEO of the site has been mapped with keywords and indexed by Google the static pages i want to have urls to have the extention .html
I had a look at the Document here
But am not quite sure how to achieve this in the right way.
Any one who has worked on it can give some pointers?
simply put this line into your Router Router::parseExtensions('html');
This will tell the Router to cut off the .html as an Extension and parse what remains.
To Create correct Links to the Pages you have to give the Link() function another Parameter called "ext".
Like this:
$this->Html->link(
'Super Seo link',
array(
'controller' => 'anyController',
'action' => 'someAction',
'title' => 'seo-title-for-gods-sake',
'ext' => 'html'
) );
Have fun! Florian

CakePHP customize Router URL order

I'm using CakePHP to one of my project. When I was designing the URL, I know CakePHP does well when I use
$this->Html->Link('Add Post', array('controller'=>'posts', 'action'=>'edit', 1234))
CakePHP will generate URI /appname/posts/edit/1234. But what I'm thinking /appname/post/1234/edit is better than the former one. I couldn't find a solution to workaround with it.
I've tried
$this->Html->Link('Add Post', array('controller'=>'posts', 1234, 'action'=>'edit'))
but CakePHP will ignore the order of array.
Does anyone know how to achieve this URL? Basically I don't want to modify function Router::url, what I need is to write code in my ctp view file and CakePHP to know the right order I put. How can I do that?
BTW, I'm using CakePHP 2.3.
This is a job for routing, more specifically reverse routing : http://book.cakephp.org/2.0/en/development/routing.html
In app/Config/routes.php you could add something along the lines of the following:
Router::connect(
'/:controller/:id/:action',
array('controller' => 'posts', 'action' => 'edit'),
array('id' => '[0-9]+')
);
Here, you tell Cake to reverse route any request coming from the edit method of the posts controller by defining the order of pre-route named parameters. In your view, the link should be constructed like this:
echo $this->Html->link('Add Post', array(
'controller' => 'posts',
'action' => 'edit',
'id' => 1234
));
Note the extra id parameter being passed that we assigned in routes.php. The Url should look like:
http://appdomain.com/posts/1234/edit
In my humble opinion, this is considered bad practice both from a conventional and SEO standpoint.

CakePHP beginner - MVC relationship

I'm a beginner in Cake and have gone through the MVC relationship. I've been given the code below to breakdown and I seem not to grasp the hang of it.
<?php echo $html->link($view['User']['nickname'], array('controller' => 'users', 'action' => 'profile', $view['User']['nickname'])); ?>
Any ideas ?
Thanks.
The code given is to generate a <a href=....> link inside a View using the HtmlHelper.
In this case, a link is generated to the 'profile' action of the 'users' controller and the 'nickname' of the user is passed as argument. The resulting link will (with a standard route configuration), look like this;
<a href='/users/profile/someNickName'>someNickName</a>
However, this code is for older versions of CakePHP (1.3 and older), for newer versions, this is the notation;
echo $this-Html->link($view['User']['nickname'], array('controller' => 'users', 'action' => 'profile', $view['User']['nickname']));
Documentation on the HtmlHelper (for CakePHP 1.3) can be found here:
CakePHP HtmlHelper documentation
However, if you're struggling with this kind of questions, I really urge you to read the CakePHP 'CookBook' from the beginning, and preferably start using CakePHP 2.x:
CakePHP Cookbook - Welcome

CakePHP route URL not found!

I am trying to do some custom routing on my site, but have been stuck for 2 days at a very silly issue. I have the following route configuration:
Router::connect('/your-solution/add-comment/*', array('controller' => 'comments', 'action' => 'add'));
Router::connect('/admin/your-solution/add-comment/*', array('controller' => 'comments', 'action' => 'add', 'admin' => true));
The problem is that when I try to load a URL formatted using the second route, it gives me a 404 not found.
The first rule works fine.
For both rules I have a separate element containing a form and pointing to a URL formatted after the respective rule. The only parameter for both actions is the solution id, which is "contained" in the wildcard.
What could possibly be the issue? Thank you very much for your help!
EDIT:
I found out another weird behaviour. When I access /admin/your-solution/add-comment/3, it goes to that action. But if I submit a form to that link, it displays a blank page, with Firebug informing me that the page was not found. Very strange...
Also, I have a similar route for editing comments. Both loading the edit form and saving the form work...
how are you?
In order to see exactly why isn't it working, go to your /app/config/core.php and seek for this line:
Configure::write('debug', 2);
And make sure the value is set to "2". This way, it'll no longer give you a 404 error, but the actual issue, since in production mode (debug set to 0), all errors are masked with a 404 error.
Let me know!
Cheers!
In your core.php be sure
Configure::write('Routing.prefixes', array('admin'));
In your comments controller, be sure you have
function admin_add() {...}
Also try other ways of formatting Routing statement.
Router::connect('/admin/your-solution/add-comment', array('controller' => 'comments', 'action' => 'add', 'admin' => true));
The order of your route is also important. You may want to check that.
For debugging which route you are using when loading the URL, try adding this code to your app_controller.php file.
function __construct() {
$route = Router::currentRoute();
pr($route);
}
These are just some tips to hopefully help you move forward.
Apparently, the problem has been lying in a disabled input. After I've deleted this element, the form submits correctly and the target page is shown.
Just for my knowledge, why didn't the form submit if it had a disabled input in it?

cakephp reverse routing conflicts with pagination logic

I've a new question :)
I'll briefly explain what I'm trying to achieve. Right now I have an url that looks like this.
/products/index/brand:figleaves
I want this to look like this
/brand/figleaves
By writing the following route rule I get what I want.
Router::connect('/brand/:brand/*', array('controller' => 'products', 'action' => 'index'));
Everything goes fine, but then I discovered the pagination logic has been destructed.
If I click on 'next page' I get redirected to the url /products/index/page:2.
it doesn't pass the brand parameter
it redirects back to the products_controller and not to the url I defined in the route rule.
In fact I'd need this as url /brand/figleaves/page:2.
Strange thing is if I browse to /products/index/brand:figleaves and click on Next, then I get redirected to /brand/figleaves/page:2. How can this be explained?
I'd appreciate some help with this :)
Kind Regards,
Laurent
For those interested in how I solved this.
I just defined some options in the paginator in my view and passed the value explicitly, like this.
$this->Paginator->options(array
('url'=> array(
'controller' => 'products',
'action' => 'index',
'brand'=>$this->params['brand']
)));
That does the job :)

Resources