Faiyaz Alam
Faiyaz Alam

Reputation: 1227

defined route is missing - Missing Route in Cakephp 3.4

I am trying to use the prefix "student". When I create a link in template or layout file I am getting this error as shown in the image: enter image description here

code in routes.php

    <?php
use Cake\Core\Plugin;
use Cake\Routing\RouteBuilder;
use Cake\Routing\Router;
use Cake\Routing\Route\DashedRoute;

Router::defaultRouteClass(DashedRoute::class);

Router::scope('/', function (RouteBuilder $routes) {
    $routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
    $routes->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
    $routes->fallbacks(DashedRoute::class);
});


Router::prefix('admin', function ($routes) {
   $routes->connect('/users/', [ 'controller' => 'MyUsers', 'action' => 'index', 'plugin'=>false]);
   $routes->connect('/login', [ 'controller' => 'MyUsers', 'action' => 'login', 'plugin'=>false, 'prefix'=>'admin']);
   $routes->connect('/', [ 'controller' => 'MyUsers', 'action' => 'dashboard', 'plugin'=>false]);
     $routes->fallbacks(DashedRoute::class);
});

Router::prefix('trainer', function ($routes) {
   $routes->connect('/users/', [ 'controller' => 'MyUsers', 'action' => 'index', 'plugin'=>false]);
   $routes->connect('/login', [ 'controller' => 'MyUsers', 'action' => 'login', 'plugin'=>false]);
   $routes->connect('/', [ 'controller' => 'MyUsers', 'action' => 'dashboard', 'plugin'=>false]);
     $routes->fallbacks(DashedRoute::class);
});


Router::prefix('student', function ($routes) {
   $routes->connect('/courses/', array ( 'controller' => 'Courses', 'action' => 'index', 'plugin' => false, 'prefix' => 'student', '_ext' => NULL, ));
   $routes->connect('/', array ( 'controller' => 'MyUsers', 'action' => 'dashboard', 'plugin' => false));
});

/**
 * Load all plugin routes.  See the Plugin documentation on
 * how to customize the loading of plugin routes.
 */
Plugin::routes();

layout file student.ctp has only one line of code:

<li><?php echo $this->Html->link('Courses', [ 'controller' => 'Courses', 'action' => 'index', 'plugin' => false, 'prefix' => 'student', '_ext' => NULL, ]);?></li>

AppController.php:

    <?php

namespace App\Controller;

use Cake\Controller\Controller;
use Cake\Event\Event;
use  Cake\Core\Configure;
use Cake\Network\Exception\NotFoundException;




class AppController extends Controller
{

    public $helpers = array(
        'CakeDC/Users.AuthLink',
        'CakeDC/Users.User',
        );


    public function initialize()
    {
        parent::initialize();

        $this->loadComponent('RequestHandler');
        $this->loadComponent('Flash');
        $this->loadComponent('CakeDC/Users.UsersAuth');
        $this->loadComponent('Utils.GlobalAuth');

         $this->Auth->config('loginRedirect', array('controller'=>'Courses', 'action'=>'index', 'plugin'=>FALSE));
         $this->Auth->config('logoutRedirect', array('controller'=>'MyUsers', 'action'=>'login', 'plugin'=>FALSE));
         $this->Auth->config('unauthorizedRedirect', array('controller'=>'Courses', 'action'=>'index', 'prefix'=>$this->Auth->user('role')));
        $this->Auth->config('loginAction', array('controller'=>'MyUsers', 'action'=>'login'));  
         $this->Auth->allow(['login', 'logout']);



    }


    public function beforeRender(Event $event)
    {
        if (!array_key_exists('_serialize', $this->viewVars) &&
            in_array($this->response->type(), ['application/json', 'application/xml'])
        ) {
            $this->set('_serialize', true);
        }

        $this->_renderLayout();
    }

    private function _renderLayout()
    {
        $prefix =  isset($this->request->params['prefix'])?$this->request->params['prefix']:FALSE;

        if(!$prefix)
        {
            return;
        }

       $this->viewBuilder()->setLayout($prefix);



    }





}

I have checked this solution : CakePHP 3: Missing route error for route that exists

Upvotes: 1

Views: 768

Answers (1)

ndm
ndm

Reputation: 60503

You cannot feed the special plugin key with a boolean, it must either be null, or a string with the name of the plugin.

Also there is no need to define the plugin or prefix keys when connecting routes, the Router::prefix() method will take care of adding the prefix. Similarily Router::plugin() will add the plugin name, and when not using Router::plugin(), a default of null is being assumed for the plugin key.

Furthermore defining _ext with null only makes sense if you want to disallow generating URLs with extensions. And specifying it when generating URLs is only neccessary when it has been defined as a non-null value, which is also true for the plugin key (unless you need to break out of the current plugin context).

Long story short, connecting the route only requires the controller and action keys:

$routes->connect('/courses/', [
    'controller' => 'Courses',
    'action' => 'index'
]);

And generating the URL only needs the additonal prefix key, plugin is optional if not used in plugin context:

$this->Html->link('Courses', [
    'controller' => 'Courses',
    'action' => 'index',
    'plugin' => null,
    'prefix' => 'student'
]);

See also

Upvotes: 2

Related Questions