On Jun 15, 2:31 am, mirza ameer <[email protected]> wrote:
> Hi,
>
> i did not understand can you please tel me how to do this iam wating
> for your reply

Mirza, a "slug" is a part of a URL that denotes the name of some item,
person, title of article, etc. The spaces are translated to a hyphen
or underscore, punctuation is generally removed, "special" characters
are translated to ASCII, and the entire string is usually shifted to
all lowercase. So, if we had a members page for Mirza Ameer the URL
might look like:

/members/mirza-ameer

In your case, the names of the products can be used for slugs. The
database table will require an extra column for the slug.

To have Cake create the slugs automatically, download
SluggableBehavior from here:
http://cake-syrup.sourceforge.net/ingredients/sluggable-behavior/

See the tutorial for more info:
http://bakery.cakephp.org/articles/view/slug-behavior

in routes.php:

Router::connect(
        '/products',
        array('controller' => 'products', 'action' => 'index')
);

/* If you have other (non-admin) methods/actions in
ProductsController, you should
 * list them before this next route. Otherwise, the name of your
action
 * will be mistaken for a slug.
 */
Router::connect(
        '/products/:slug',
        array('controller' => 'products', 'action' => 'view'),
        array('slug' => '[-a-z]+', 'pass' => array('slug'))
);

The regular expression will match on an lowercase word consisting of
hyphens and letters only. If your product names might include numbers,
you should change the regexp to:
[-a-z0-9]+

Note the "pass" part. This tells Cake that you expect the slug to be
passed as a parameter to your method.

in product.php (model):

var $actsAs = array(
        'Sluggable' => array(
                'translation' => 'utf-8',
                'separator' => '-',
                'label' => array('name'),
                'length' => 64,
                'overwrite' => true
        )
);

in products_controller.php:

public function view($slug = null)
{
        if (empty($slug))
        {
                $this->redirect(array('action' => 'index'));
        }

        $data = $this->Product->find(
                'first',
                array(
                        'conditions' => array(
                                'Product.slug' => $slug
                        )
                )
        );

        $this->set(compact('data'));
}


To create a link to a particular product:

echo $html->link(
        $d['Product']['name'],
        array(
                'controller' => 'products',
                'action' => 'view',
                'slug' => $d['Product']['slug']
        ),
        array('title' => 'click to see this product')
);

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to