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
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.