0

I have been trying to get pretty url with the router with Cake.

www.mysite.com/category/slug

www.mysite.com/slug

www.mysite.com/category/

I think there is no formula to get exactly what I want so I want explicitly match each url. I want my route table to look like this

  id | url | controller | action | param 

So when someone goes to www.mysite.com/mysubcategory/my-title-of-page

I want it to cross reference "mysubcategory/my-title-of-page" with the url column and proceed. What is the best way to do this?

My guess would be to be to direct all traffic to a Route Controller

Router::connect('/*', array('controller' => 'Routes', 'action' => 'redirs'));

But when it redirects i want it to show the pretty url in the browser, i dont want it to show articles/view/12

thank you

Moppo
  • 18,797
  • 5
  • 65
  • 64
artSir
  • 540
  • 1
  • 8
  • 29

1 Answers1

1

You could either dynamically create your routes with the information retrieved from the database, or use a custom route class that looks up the table. If you only have a few routes, then you may want to use the former.

In your routes.php simply read your table and connect the routes, it should be as simple as that. Here's a very basic example:

use Cake\ORM\TableRegistry;

$Routes = TableRegistry::get('Routes');
$routes = $Routes->find();

foreach ($routes as $route) {
    Router::connect(
        $route->url,
        [
            'controller' => $route->controller,
            'action' => $route->action
        ],
        [
            'pass' => [$route->param]
        ]
    );
}

For something dynamic, check for example Mapping slugs from database in routing

ndm
  • 59,784
  • 9
  • 71
  • 110