cakephp - CakePHP3.x controller name in url when using prefix routing -
i trying use prefix routing in cakephp3. added following lines /config/routes.php.
router::prefix("admin", function($routes) { // routes here prefixed ‘/admin‘ // , have prefix => admin route element added. $routes->connect("/",["controller"=>"tops","action"=>"index"]); $routes->connect("/:controller", ["action" => "index"]); $routes->connect("/:controller/:action/*"); });
after that, created /src/controller/admin/questionscontroller.php below.
<?php namespace app\controller\admin; use app\controller\appcontroller; class questionscontroller extends appcontroller { public function index() { //some code here } } ?>
finally tried access localhost/app_name/admin/questions/index
, got error saying, error: questionscontroller not found
. however, when capitalize first letter of controller name(i.e. localhost/app_name/admin/questions/index), working fine. thought weird because without prefix, can use controller name first character not capitalized. kind of bug?
in cake 3.x, routes not inflect default anymore, instead you'll have explicitly make use of inflectedroute
route class, can example bee seen in default routes.php
app configuration:
router::scope('/', function($routes) { // ... /** * connect route index action of controller. * , more general catch route action. * * `fallbacks` method shortcut * `$routes->connect('/:controller', ['action' => 'index'], ['routeclass' => 'inflectedroute']);` * `$routes->connect('/:controller/:action/*', [], ['routeclass' => 'inflectedroute']);` * * can remove these routes once you've connected * routes want in application. */ $routes->fallbacks(); });
you custom routes not specify specific route class, default route
class being used, while fallback routes make use of inflected routing, , why it's working without prefix.
so either use capitalized controller names in url, or use route class inflectedroute
transforms them properly:
router::prefix('admin', function($routes) { // routes here prefixed ‘/admin‘ // , have prefix => admin route element added. $routes->connect( '/', ['controller' => 'tops', 'action' => 'index'] ); $routes->connect( '/:controller', ['action' => 'index'], ['routeclass' => 'inflectedroute'] ); $routes->connect( '/:controller/:action/*', [], ['routeclass' => 'inflectedroute'] ); });
see http://book.cakephp.org/3.0/en/development/routing.html#route-elements
Comments
Post a Comment