Sunday, February 13, 2022

[FIXED] cakephp3- controller could not be found

Issue

I'm learning cakephp 3 framework. Its better to say I'm pretty much new at this.

I'm trying to make a plugin. I baked a plugin like this bin/cake bake plugin myFirstPlugin Then I baked a controller for this plugin using

bin/cake bake controller --plugin myFirstPlugin test

So inside my plugin's controller, I defined a function which does nothing other than outputting a message.

This is my plugin's controller

<?php
namespace myFirstPlugin\Controller;

use myFirstPlugin\Controller\AppController;

class testController extends AppController
{
    public function index()
    {
        $msg = 'test';
        $this->set(compact('msg'));
        $this->set('_serialize', ['msg']);
    }
}
?>

and in my application's route (not in plugin's route) I wrote this:

Router::scope('/myFirstPlugin/index', function (RouteBuilder $routes){  
    $routes->connect('/', ['controller' => 'test', 'action' => 'index']);
}); 

So, when I try to call this using the below url

http://localhost/myApplication/myFirstPlugin/index

I got this error:

Error: testController could not be found.
Error: Create the class testController below in file: src\Controller\testController.php

Why am I getting such an error even though I created a controller. Thanks!


Solution

You need to mention your plugin name within $routes->connect

Router::scope('/myFirstPlugin/index', function (RouteBuilder $routes){  
    $routes->connect('/', ['plugin' => 'myFirstPlugin','controller' => 'test', 'action' => 'index']);
}); 


Answered By - Object Manipulator

No comments:

Post a Comment

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