In core\cake\libs\controller\images_controller.php, I have this class:

<?php
class ImagesController extends AppController
{
    ....................
    function view()
    {
        ....................
    }
    ....................
}
?>

The problem is that I needed to modify the behavior of ImagesController::view() but in order to do that, I modified core\cake\libs\controller\images_controller.php directly. Not a good practice to modify core\cake\. I should add code to app\ instead, overriding classes in CakePHP. What I am planning to do is to revert changes I made to core\cake\libs\controller\images_controller.php, specifically to the ImagesController::view() method. Then, I am planning to add a controller file named images_controller.php to the /app/controllers directory. This class would extend the ImagesController in core\cake\libs\controller\images_controller.php, by just adding the new view() function, which would override the view() function in the core. I think using the same class name in both the app\ and the core\cake\ is ok, but I am not sure. I can try that. In /app/controllers, all of my classes extend from AppController. For example:

class UsersController extends AppController{
    ....................
}

But now, it seems to me that in /app/controllers, I would have to create a class like this:

class ImagesController extends ImagesController{
    ....................
}

Basically, the ImagesController class that I am trying to create in /app/controllers, should extend from the ImagesController class that I have in core\cake\libs\controller\images_controller.php, meaning extending/inheriting from this class in core\cake\libs\controller\images_controller.php:

<?php
class ImagesController extends AppController
{
    ....................
    function view()
    {
        ....................
    }
    ....................
}
?>

class ImagesController extends ImagesController does not look right to me because I am not sure if CakePHP would understand that I am trying to say that the name of my class in /app/controllers will be ImagesController and that I am trying to extend it from a class called ImagesController that is defined in core\cake\libs\controller\images_controller.php. How could you achieve that? Thank you.

2

There are 2 best solutions below

0
ndm On BEST ANSWER

As mentioned in the comments, the CakePHP core doesn't ship with an ImagesController class, meaning the core has been modified, and the file shouldn't have been placed there in the first place.

As long as this was a simple mistake, and this isn't bound to other modifications that rely on the file being present in the core folder, it should be possible to simply move it into the application level controllers folder, ie to app/controllers/images_controller.php.

7
Don Drake On

To address the part of your question that involves extending a same-name class, you would do something like this:

namespace App\Controller

use Foreign\Namespace\Controller\SameNameController as DifferentName

class SameNameController extends DifferentName {

//code here

}

[more details here][https://www.php.net/manual/en/language.namespaces.importing.php]