skip to Main Content

I am new for cakephp. I am trying to rewrite the url to make it SEO friendly.

I have create module in cakephp3 for cms page.

Page Table – url field would like to use as “about-us”

CREATE TABLE IF NOT EXISTS `pages` (
  `id` int(11) NOT NULL,
  `title` varchar(100) NOT NULL,
  `detail` text NOT NULL,
  `url` varchar(225) NOT NULL,
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=latin1;

Current URL (working)-
https://example.com/pages/view/8

I want to make this like below.
https://example.com/about-us

Please suggest.

2

Answers


  1. In your cakephp3 folder, there is a file “routes.php” in config.

    enter image description here

    Open the “routes.php” and use:

    use CakeRoutingRouter;
    
    // Using the scoped route builder.
    Router::scope('/', function ($routes) {
        $routes->connect('/about-us', ['controller' => 'Pages', 'action' => 'view', 8]);
    });
    
    // Or using the static method.
    Router::connect('/about-us', ['controller' => 'Pages', 'action' => 'view', 8]);
    
    Login or Signup to reply.
  2. in side route::

    Router::scope('/', function ($routes) {
    $routes->connect('/:title', ['controller' => 'Pages', 'action' =>  'home'],['pass' => ['title']]);
    });
    

    //in PagesController

    public function home($title=NULL){ 
            echo $title;//title = 'yogendra'
    }
    

    browser url:: http://localhost/project-name/yogendra
    you can compare title with you DB table and display page dynamically
    Hope you fixed the issue. 🙂

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search