Connor Peet
Connor Peet

Reputation: 6265

Target Class is not instantiable

I'm building a small REST API with Laravel 4. I'm using an HMVC scheme in the application. The issue is, when I try to call a controller for the API, PHP says that the class is not instantiable.

Target [App\Modules\ChunkletAPI\v1\ServerController] is not instantiable.

Here's the class itself, in v1/controllers:

<?php namespace App\Modules\ChunkletAPI\v1;

class ServerController extends ChunkletAPI {}

Which inherits from

<?php namespace App\Modules\ChunkletAPI\v1;

use Controller;

abstract class ChunkletAPI extends Controller {

    protected $name;

    protected function __construct() {
        $this->name = '\Model\ ' . str_replace('Controller', '', get_class($this));
    }

    public function index() {
        $n = $this->name;
        return $n::all();
    }
}

The routing is done by:

<?php namespace App\Modules\ChunkletAPI;

use \Illuminate\Support\Facades\Route;

Route::group(array('prefix' => 'api/v1'), function()
{
    Route::resource('server', 'App\Modules\ChunkletAPI\v1\ServerController');
});

I cannot figure out what is occurring - I've tried playing around, making the parent class non-abstract, etc - and Google is no help. Any ideas?

Upvotes: 1

Views: 6366

Answers (1)

Connor Peet
Connor Peet

Reputation: 6265

Found out why. ChunkletAPI->__construct() had to be set as public, not protected. Otherwise, it looked to Laravel like a static class.

Upvotes: 5

Related Questions