On Sat, Feb 19, 2011 at 10:31 PM, josef corley <[email protected]> wrote:
> hello everyone,
>
> when I submit a post that has a title with a (.) dot at the end, the
> url created by HTMLHelper is without the (.) dot at the end, making
> the link invalid...
>
> how can I manage dots(.) at the end of urls?
>
> example:
>
> "localhost/johto/posts/view/The%20intersection%20....."
>
> cakephp's HtmlHelper makes it like this:
>
> "localhost/johto/posts/view/The%20intersection"

You should be using a slug, not the actual title. It's a cinch to set
up with SluggableBehavior. That'll transform any spaces and other
special characters to a hyphen (or underscrore, if you prefer).

http://bakery.cakephp.org/articles/mariano/2007/03/24/sluggable-behavior

Add a column, slug, to your table (same size as the title column).
Then load SluggableBehavior in the Post model. When you save a record,
the slug will be created.

You can also choose to leave out the "view" part in the URL by
creating a route like so:

Router::connect(
        '/blog/:slug',
        array(
                'controller' => 'posts',
                'action' => 'view',
        ),
        array(
                'slug' => '[-a-z0-9]+',
                'pass' => array('slug')
        )
);

But, if you do, be sure to create routes for all your other post
method before that one. Otherwise, the actions will be confused for
slugs.

Then, to create links:

foreach($data as $d)
{
?>
        <div class="Post">
                <div class="PostHead">
                        <h3>
                                <?= $this->Html->link(
                                        $d['Post']['title'],
                                        array(
                                                'controller' => 'posts',
                                                'action' => 'view',
                                                'slug' => $d['Post']['slug']
                                        )
                                        array('title' => 'read more')
                                ) ?>                    
                        </h3>
                        
And define your method to accept the slug as a param:

public function slug($slug = null) { ... }


>
> NOTE:
>
> I'm still using localhost, the site is not yet uploaded.

You should consider setting up virtual hosts locally. Then adding them
to your /etc/hosts file. For example, if your local machine is called
foo,have entries in the hosts file like:

127.0.0.1       some-site.foo
127.0.0.1       other-site.foo
etc.

Then you can do away with using localhost or ~userdir in URLs.

-- 
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
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to