PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, February 20, 2022

[FIXED] cakephp 3.x routing to subfolders

 February 20, 2022     cakephp-3.0, routing     No comments   

Issue

On my website I have the usual routes for

website.com
website.com/pages/about
website.com/img/116633
...

Now I have these pages

website.com/username/mike
website.com/username/john
website.com/username/eric
...

which should become

website.com/mike
website.com/john
website.com/eric
...

my routing goes like this:

$routes->connect(
    '/*',
    ['controller' => 'username', 'action' => 'index'],
    ['routeClass' => 'DashedRoute']
);

And this works, but all other pages like

website.com/pages/about
website.com/img/116633

are broken (they take the 'controller' => 'username')

what is the best way to make this happen?

Below my full routes.php

    Router::defaultRouteClass('Route');

Router::extensions(['html', 'rss', 'pdf']);

Router::scope('/', function ($routes) {

    $routes->connect('/', ['controller' => 'Search', 'action' => 'start']);

    $routes->connect('/start', ['controller' => 'Search', 'action' => 'start']);

    $routes->connect(
        '/*',
        ['controller' => 'username', 'action' => 'index'],
        ['routeClass' => 'DashedRoute']
    );

    $routes->fallbacks('InflectedRoute');
});




Router::connect('/img/*', ['controller' => 'Pic', 'action' => 'item']);

Router::url([
    'controller' => 'Pages',
    'action' => 'lang',
    '_base' => 'false'
]);



Router::addUrlFilter(function ($params, $request) {
    if (isset($request->params['lang']) && !isset($params['lang'])) {
        $params['lang'] = $request->params['lang'];
    }
    return $params;
});


Plugin::routes();

Solution

 $routes->connect(
    '/*',
    ['controller' => 'username', 'action' => 'index'],
    ['routeClass' => 'DashedRoute']
);

has to be replaced with:

$routes->connect(
    '/:username',
    ['controller' => 'Username', 'action' => 'index'],
    [
      'pass' => ['username'],
        'username'  => '[0-9a-zA-Z]+'
    ]
);

This passes the username from the URL to the index() in the Username controller.

For more information: Route Elements and Passing parameters to Action.



Answered By - Sevvlor
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing