@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.