Custom controller to be extended by others

I created a generic class in client.php file with methods that wil be used other classes:

namespace App;

use Sober\Controller\Controller;

class Clients extends Controller
{
    
    private function getProperties($id)
    {
        ...
    }
}

How can I call this class in others controllers files?

Here is what I try in front-page.php file:

namespace App;

use App\Clients;

class FrontPage extends Clients
{
  ...
}

But it return a fatal error: Class ‘App\Clients’ not found

The statement use App\Clients; is unnecessary. You’re already in the App namespace, which means the Clients class is already accessible.

In the original controller they use use Sober\Controller\Controller; because Controller is not in the App namespace; it is in the Sober\Controller namespace.

Thanks for the explanation @alwaysblank. I remove use App\Clients; from front-page.php file, but it still returning a fatal error: Class ‘App\Clients’ not found

Then you would need to look at how your files are being autoloaded. You haven’t specified which version of Controller you’re using, which may be relevant. Composer needs to be able to autoload classes based on PSR-4 in order to make classes available.

I’m using “soberwp/controller”: “~9.0.0-beta.4”

I haven’t make any change in composer.json file. It’s exactly like the original: https://github.com/roots/sage/blob/master/composer.json

The version of Controller you’re using doesn’t autoload classes to make them available elsewhere: It just uses the class syntax to understand how to load controllers. The most recent version of Controller (2.0.1 as of this writing) does use PSR-4, and consequently enforces naming conventions that should allow you to autoload classes.

So long as you’re putting your client.php file under app/, you shouldn’t need to upgrade, so long as your class and file names follow PSR-4 standards, since Sage already tells Composer to look for App-namespaced things in app/. In this case, you would need to name your file Client.php (uppercase is important) and make sure it’s in app/.

Fixed! Change the file name to Clients.php and move it to app/, not app/controller. Thanks @alwaysblank

After doing a bit more research, I’m not sure this strategy will work the way you want it to: Controller looks for extends Controller in files to determine which ones are valid controller class, and your front-page.php would fail that test.

You might consider using Traits to accomplish the same goal.

Yeah I use traits for this kind of thing. That seems to be how @withjacoby planned the feature and I always find it safer to stick with the developer’s plans.

Thanks for the tip guys! This is how I solved my problem:

app/contollers/partials/properties.php

namespace App\Partials;

trait Properties
{
    public function GetProperties($id = false)
    {
     ...
    }
}

app/controllers/front-page.php

namespace App;

use Sober\Controller\Controller;

class FrontPage extends Controller
{
    use Partials\Properties;
...
1 Like