I don't know what you mean by "after the link is clicked" but if you
want nicer looking URLs then, yes, you need to define a route. First,
though, you should be including a "slug", not the title, in the URL.
The reason is that a slug removes spaces and other unwanted
characters. Take a look at SluggableBehavior. It's simple to set up
and use. If you already have some articles in the DB it shouldn't be a
big deal to update the records -- a simple shell script will do the
job.

Once you have a slugs set up, you need to define the route.

Router::connect(
        '/:slug',
        array(
                'controller' => 'articles',
                'action' => 'view'
        ),
        array(
                'slug' => '[0-9a-z]+',
                'pass' => array('slug')
        )
);

Remember to include the $slug param in your controller method:

public function view($slug = null) {

But there's a problem with this route, in that it's far too broad. It
would mean that *every* request to the site would try to fetch an
article. If you have any other pages on the site, you'd need to define
routes for them too, and they must come before this one.

A better approach would be to include some boilerplate string in the
URL like so:

Router::connect(
        '/articles/:slug',
        array(
                'controller' => 'articles',
                'action' => 'view'
        ),
        array(
                'slug' => '[0-9a-z]+',
                'pass' => array('slug')
        )
);

This narrows down the possibilities nicely. The string can be anything
you like, not just "articles".

To create the link:

$this->Html->link(
        $art['ArticleVersion'][0]['title'],
        array(
                'controller' => 'articles',
                'action' => 'view',
                'slug' => $art['ArticleVersion'][0]['slug']
        ),
        array('title' => 'click me')
)

I'm assuming that the slug data will be found at
$art['ArticleVersion'][0]['slug']. It seems odd that you'd have the
title like that. Is this a multilingual site?



On Tue, Aug 7, 2012 at 12:53 PM, Jana <janis.ro...@gmail.com> wrote:
> The code below is for something like a wordpress feed of articles.
> It gives me this in the url when clicked.
> http://www.examplemagazine.com/articles/view/102697
> What I want to do is after the link is clicked just give me the name of the
> article in the url after the magazine site.
> Like this http://www.examplemagazine.com/nameofarticle
> Is this a defining routes issue?  If so, do I use the
> Router::connect() method to reroute the url?
> The first value $art['ArticleVersion[0][title] is what I want displayed in
> the url.
> I don't want him to have to see the id number or the word "articles" or
> "view".
> If you give me an example or tell me if I am looking in the right place it
> would be great.
> <?= $html->link($art['ArticleVersion'][0]['title'], array(
>              'controller' => 'articles',
>               'action' => 'view',
>             $art['ArticleVersion'][0]['article_id'])); ?>
>
> tia,
>
>           </h3>
>
> --
> Our newest site for the community: CakePHP Video Tutorials
> http://tv.cakephp.org
> Check out the new CakePHP Questions site http://ask.cakephp.org and help
> others with their CakePHP related questions.
>
>
> To unsubscribe from this group, send email to
> cake-php+unsubscr...@googlegroups.com For more options, visit this group at
> http://groups.google.com/group/cake-php

-- 
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en-US.


Reply via email to