How to use SEO urls in CakePHP without the id?

by priscilla.langworth , in category: SEO , 2 years ago

How to use SEO urls in CakePHP without the id?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

3 answers

Member

by dortha , 2 years ago

@priscilla.langworth 

In CakePHP, you can use SEO-friendly URLs without the ID by utilizing the routes.php file to define custom routes. Here is an example:

1
2
3
4
5
Router::connect(
    '/:slug',
    ['controller' => 'products', 'action' => 'view'],
    ['pass' => ['slug']]
);


In this example, the /:slug URL pattern will match any URL that has a slug value following the root path. The ['controller' => 'products', 'action' => 'view'] setting maps the URL to the view action of the products controller. The ['pass' => ['slug']] setting passes the slug value to the controller action.


In your ProductsController, you'll then need to modify the view action to retrieve the product based on the slug value:

1
2
3
4
public function view($slug) {
    $product = $this->Products->findBySlug($slug);
    $this->set(compact('product'));
}


Note that you'll need to define the findBySlug method in your ProductsTable to retrieve the product based on the slug value.

by creola.ebert , 10 months ago

@priscilla.langworth 

Additionally, in order to create SEO-friendly URLs without the ID, you need to ensure that you generate the slug for each product and save it in the database. Here is an example of how to generate and save the slug:


1 2 3 4


public function beforeSave($event, $entity, $options) { if ($entity->isNew() && empty($entity->slug)) { $entity->slug = Text::slug($entity->name); } }


In this example, the beforeSave method is used to generate and save the slug before the product is saved to the database. The Text::slug method is used to create a URL-friendly slug from the product name.


Lastly, to generate the SEO-friendly URLs in your views, you can use the HtmlHelper to create links to the product view action using the slug parameter. Here is an example:


1 2


echo $this->Html->link($product->name, ['controller' => 'products', 'action' => 'view', $product->slug]);


In this example, the link method of the HtmlHelper is used to create a link to the product view action using the product slug as the parameter.


By following these steps, you can use SEO-friendly URLs without the ID in CakePHP.

Member

by delpha , 10 months ago

@priscilla.langworth 

Additionally, you can make use of the SluggableBehavior provided by the CakePHP community to automatically generate and manage slugs for your models. Here are the steps to use the SluggableBehavior in CakePHP:

  1. Install the Sluggable plugin by running the following command:
1
composer require friendsofcake/cakephp-sluggable


  1. Add the SluggableBehavior to your model's initialize method:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public function initialize(array $config)
{
    parent::initialize($config);

    $this->addBehavior('Sluggable.Sluggable', [
        'field' => 'name',
        'slugField' => 'slug',
        'overwrite' => true
    ]);
}


In this example, we are specifying the name field as the source field, slug field as the target field (where the generated slug will be saved), and overwrite as true to regenerate the slug when the source field changes.

  1. Update your routes.php file to map the slug parameter to your controller action:
1
Router::connect('/:slug', ['controller' => 'products', 'action' => 'view'], ['pass' => ['slug']]);


This will map URLs like /apple-iphone-x to the view action of the ProductsController where apple-iphone-x will be passed as the slug parameter.

  1. Implement the view action in your ProductsController to fetch the product based on the slug:
1
2
3
4
5
public function view($slug)
{
    $product = $this->Products->findBySlug($slug)->firstOrFail();
    $this->set(compact('product'));
}


In this example, we are using the findBySlug method to fetch the product based on the slug and passing it to the view.


With these steps, you can now generate SEO-friendly URLs without the ID in CakePHP using the SluggableBehavior.