CakePHP beginner - MVC relationship - cakephp

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

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 URL masking?

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.

How can make CakePhp render php content stored in a table

Is it possible to store mixed html and php content in a db table and have it rendered in views? If I have the following in the table
<?php echo $html->link('Cool product 1', array('controller' => 'products', 'action' => 'view', 1), array('target' => '_blank')); ?>
this is what get's rendered:
link('Cool product 1', array('controller' => 'products', 'action' => 'view', 1), array('target' => '_blank')); ?>
I'd like the link , not the code. How can I achieve that?
UPDATE
It's for a multi-lingual site. I'd like to store the different static pages, in different languages, in a table, instead of having them in the page view folder.
I'd like to keep my helper code, so that I can create and test my pages locally before putting them online, and not having to worry about urls.
I'd like to put them in a table as it would simplify my code. I could link them to my site hierarchy, that is in a table.
Either store the link as HTML in your database or you could try using Eval to run the code.
Update: To get around the problem with the opening PHP tag you can try using eval like this: eval('?>' . $content);
But I think it's a bad idea to store plan PHP code into your database. Maybe explaining what you would like to achieve and why you are using this approach would help getting better answers.
Someone said "if eval is the answer, the question was wrong". You should consider to rethink what you are trying to achieve.

Use of Routing in CakePHP

Why we are using routing in cakePHP and what would be the basic approaches for implementation...?
Routing allows aliasing and routing(!) of URLs. It gives us a cleaner, more controlled interface and underpins the functioning of CakePHP.
The first step would be to read the appropriate chapter in the book: http://book.cakephp.org/view/542/Defining-Routes (1.2) or http://book.cakephp.org/view/948/Defining-Routes (1.3)
Then look at the routes.php file (app/config/routes.php) to understand how it goes together.
Finally, when you know what you want to do (we don't because you haven't told us), try it debug it and use it.
Why
Because it allows you to decouple your URLs from your controller actions. You can name your controllers and actions in a way that makes sense internally, and invoke them using URLs that do not need to bear any resemblance to your internal naming scheme.
FooApiVersion1Controller::internal_beta_method() can be invoked by the URL /api/v1/method, and you can swap out the controller or method at any time without needing to change the URL.
How
Read the manual. http://book.cakephp.org/view/945/Routes-Configuration
http://book.cakephp.org/view/310/Configuration
<?php
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
?>

Resources