Slim Callable UserController 不存在 RuntimeException
Slim Callable UserController does not exist RuntimeException
嗨,我是 slim 的新手,我一直坚持这个,请大家帮忙
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;
}
}
和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};
}
}
正如您将路线定义为:
$app->get('/', 'UserController:index');
然后你需要将你的DI工厂定义为:
$container['UserController'] = function ($container) {
// return an instantiated UserController here.
};
您还应该查看名称空间和 PSR-4 的名称空间名称到目录的映射是如何工作的。通常,命名空间名称中永远不会有 src
,但是您确实看到 App
之类的命名空间映射到 Composer 自动加载器中的目录调用 src
,如 [=42= 中指定的那样].
通常,它看起来像这样:
"autoload": {
"psr-4": {
"App\": "src/"
}
},
这意味着您有一个名为 src
的目录,该目录中的任何 class 都将具有 App
的基本命名空间,然后任何其他目录都将作为 sub-namespaces.
即如果您有一个名为 src/Controllers/UserController.php
的文件,那么该文件中的 class 定义将是:
<?php
namespace App\Controllers;
class UserController
{
// methods here
}
另请注意,文件名的大写与 class 名称匹配,目录的大写与 sub-namespaces 的大写匹配。
继续这个例子,我希望 DI 工厂看起来像这样:
$container['UserController'] = function ($container) {
return new \App\Controllers\UserController($container->get('settings'));
};
在命名空间定义中看到 src
确实很不寻常,因此请检查并检查您的命名空间和磁盘上的文件是否完全匹配,因为问题中的代码不一致。
您可以检查包含控制器 class 的文件 (controller.php) 的名称。它们必须相同。
例如:
如果您有 Contoller.php 文件,您必须像这样 Controller 创建 class 的名称。
在您的示例中,您的 Controller.php 文件的大写字母为 C
但是,class controller 不是以大写字母开头的。
所以,确保文件名与class名称.
相同
希望这对您有所帮助。
就我而言,这是可行的,我希望你也能如此
home_controller.php
namespace Apa\Kabar;
use Psr\Container\ContainerInterface;
class HomeController
{
protected $container;
// constructor receives container instance
public function __construct(ContainerInterface $container) {
$this->container = $container;
}
public function home($request, $response, $args) {
// your code
// to access items in the container... $this->container->get('');
return $response->write('apa kabar');
}
public function contact($request, $response, $args) {
// your code
// to access items in the container... $this->container->get('');
return $response;
}
}
route.php*
require __DIR__. '/../controller/home_controller.php';
$configuration = [
'settings' => [
'displayErrorDetails' => true,
],
];
$con = new Container($configuration);
$app = new App($con);
$con = $app->getContainer();
$con['HomeController'] = function($con) {
return new HomeController($con);
};
$app->get('/home', 'HomeController:home');
嗨,我是 slim 的新手,我一直坚持这个,请大家帮忙
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;
}
}
和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};
}
}
正如您将路线定义为:
$app->get('/', 'UserController:index');
然后你需要将你的DI工厂定义为:
$container['UserController'] = function ($container) {
// return an instantiated UserController here.
};
您还应该查看名称空间和 PSR-4 的名称空间名称到目录的映射是如何工作的。通常,命名空间名称中永远不会有 src
,但是您确实看到 App
之类的命名空间映射到 Composer 自动加载器中的目录调用 src
,如 [=42= 中指定的那样].
通常,它看起来像这样:
"autoload": {
"psr-4": {
"App\": "src/"
}
},
这意味着您有一个名为 src
的目录,该目录中的任何 class 都将具有 App
的基本命名空间,然后任何其他目录都将作为 sub-namespaces.
即如果您有一个名为 src/Controllers/UserController.php
的文件,那么该文件中的 class 定义将是:
<?php
namespace App\Controllers;
class UserController
{
// methods here
}
另请注意,文件名的大写与 class 名称匹配,目录的大写与 sub-namespaces 的大写匹配。
继续这个例子,我希望 DI 工厂看起来像这样:
$container['UserController'] = function ($container) {
return new \App\Controllers\UserController($container->get('settings'));
};
在命名空间定义中看到 src
确实很不寻常,因此请检查并检查您的命名空间和磁盘上的文件是否完全匹配,因为问题中的代码不一致。
您可以检查包含控制器 class 的文件 (controller.php) 的名称。它们必须相同。
例如:
如果您有 Contoller.php 文件,您必须像这样 Controller 创建 class 的名称。
在您的示例中,您的 Controller.php 文件的大写字母为 C 但是,class controller 不是以大写字母开头的。
所以,确保文件名与class名称.
相同希望这对您有所帮助。
就我而言,这是可行的,我希望你也能如此
home_controller.php
namespace Apa\Kabar;
use Psr\Container\ContainerInterface;
class HomeController
{
protected $container;
// constructor receives container instance
public function __construct(ContainerInterface $container) {
$this->container = $container;
}
public function home($request, $response, $args) {
// your code
// to access items in the container... $this->container->get('');
return $response->write('apa kabar');
}
public function contact($request, $response, $args) {
// your code
// to access items in the container... $this->container->get('');
return $response;
}
}
route.php*
require __DIR__. '/../controller/home_controller.php';
$configuration = [
'settings' => [
'displayErrorDetails' => true,
],
];
$con = new Container($configuration);
$app = new App($con);
$con = $app->getContainer();
$con['HomeController'] = function($con) {
return new HomeController($con);
};
$app->get('/home', 'HomeController:home');