Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slim Callable UserController does not exist RuntimeException

Hi i am new to slim i stuck on this anyone help please

routes.php

$app->get('/', 'UserController:index');

dependencis.php

$container['src\UserController'] = function ($container) {
    return new \src\UserController($container->get('settings'));
};

UserController.php

namespace App\Controllers;

use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use App\src\Controller;
class UserController extends Controller {
    public function index(Request $request, Response $response) {
        return $this->db;
    }
}

and controller.php

namespace App\src;

class Controller {
    protected $container;
    public function __construct($c) {
        $this->container = $c;
    }

    public function __get($property) {
        if($this->container->has($property)) {
            return $this->container->get($property);
        }
        return $this->{$property};
    }
}
like image 674
Shahzad Nasir Avatar asked Feb 14 '17 07:02

Shahzad Nasir


2 Answers

As you have defined your route as:

$app->get('/', 'UserController:index');

Then you need to define your DI factory as:

$container['UserController'] = function ($container) {
    // return an instantiated UserController here.
};

You should also look up how namespaces and PSR-4's mapping of namespace name to directory work. In general there is never a src in a namespace name, but you do see a namespace such as App mapped to a directory call src in the Composer autoloader as specified in composer.json.

Usually, it looks something like this:

"autoload": {
    "psr-4": {
        "App\\": "src/"
    }
},

This means that you have a directory called src and any class inside that directory, will have a base namespace of App and then any other directories act as sub-namespaces.

i.e. if you have a file called src/Controllers/UserController.php, then the class definition in that file will be:

<?php
namespace App\Controllers;
class UserController
{
    // methods here
}

Note also that the capitalisation of the filename matches the class name and that the capitalisation of the directories match the capitalisation of the sub-namespaces.

To continue the example, I would expect the DI factory to look like this:

$container['UserController'] = function ($container) {
    return new \App\Controllers\UserController($container->get('settings'));
};

It's really unusual to see src in a namespace definition, so go through and check that your namespaces and files on disk all match up as the code in the question is inconsistent.

like image 130
Rob Allen Avatar answered Nov 11 '22 07:11

Rob Allen


You can check the name of the file (controller.php) that holds controller class. they must be the same.

for example :

if you have Contoller.php file you must make the name of class like this Controller.

in your example you have Controller.php file with capital letter of C but, the class controller doesn't start with a capital letter.

so, make sure the file name is same as the class name.

hope this will help you .

like image 26
Mohammed_7aafar Avatar answered Nov 11 '22 07:11

Mohammed_7aafar