Slim框架消息:Callable Homecontroller不存在
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Slim框架消息:Callable Homecontroller不存在相关的知识,希望对你有一定的参考价值。
公共/ index.php文件
<?php
require __DIR__ . '/../vendor/autoload.php';
ini_set('display_errors', 'On');
if (PHP_SAPI == 'cli-server') {
// To help the built-in PHP dev server, check if the request was actually for
// something which should probably be served as a static file
$url = parse_url($_SERVER['REQUEST_URI']);
$file = __DIR__ . $url['path'];
if (is_file($file)) {
return false;
}
}
session_start();
// Instantiate the app
$settings = require __DIR__ . '/../src/settings.php';
$app = new SlimApp($settings);
// Set up dependencies
require __DIR__ . '/../src/dependencies.php';
// Register middleware
require __DIR__ . '/../src/middleware.php';
// Register routes
require __DIR__ . '/../src/routes.php';
// Run app
$app->run();
这里我们有composer.json
{
"autoload":{
"psr-4": {
"App\": "src"
}
},
"require": {
"php": ">=5.5.0",
"slim/slim": "^3.1",
"slim/php-view": "^2.0",
"monolog/monolog": "^1.17",
"illuminate/database": "~5.1",
"slim/twig-view": "^2.3"
},
"require-dev": {
"phpunit/phpunit": ">=4.8 < 6.0"
},
"autoload-dev": {
"psr-4": {
"Tests\": "tests/"
}
},
"config": {
"process-timeout" : 0
},
}
这是我的控制器,我获得了命名空间的错误,在我看到的所有示例中使用了这个...我不知道该怎么做更多
<?php
namespace AppControllers;
class Homecontroller
{
protected $container;
// constructor receives container instance
public function __construct(ContainerInterface $container) {
$this->container = $container;
}
public function home($request, $response, $args) {
echo "locura";
// your code
// to access items in the container... $this->container->get('');
return $response;
}
public function contact($request, $response, $args) {
// your code
// to access items in the container... $this->container->get('');
return $response;
}
}
实际配置在我的routes.php中,我把它放在:
$app->get('/new', Homecontroller::class . ':home');
配置之前
在上面的代码中,我尝试将控制器创建到容器中,然后使用
$app->get('/new', 'HomeController:home');
在dependencies.php我把这个代码:
$container['HomeController'] = function($c) {
$view = $c->get("view"); // retrieve the 'view' from the container
return new HomeController($view);
};
但我没有获得任何配置的任何结果
我想从路由器加载HomeController
这是我放api.powertv / new时的错误
Type: RuntimeException
Message: Callable Homecontroller does not exist
File: /Users/alfonso/Sites/powertv_api/vendor/slim/slim/Slim/CallableResolver.php
Line: 90
我来到这里,我把这篇文章当作我最后的资源,如果失败我不知道我要做什么。
答案
尝试在您的:中使用绝对命名空间路径:
- dependencies.php $ container ['yourController'] = function($ c){$ view = $ c ['view'];返回new App Controllers YourController($ view); }
- routes.php文件 $ app-> get('your_defined_route_name', App Controllers YourController :: class。':YourControllerMethod');
另一答案
容器内的名称和你在路径中使用的thoose必须匹配你已经将容器键设置为HomeController
并在路径中使用了HomeController
,这些字符串不相等。
要么自己创造字符串
$container['HomeController'] = function($c) {
$view = $c->get("view"); // retrieve the 'view' from the container
return new HomeController($view);
};
$app->get('/new', 'HomeController:home');
或者使用::class
语法:
$container[HomeController::class] = function($c) {
$view = $c->get("view"); // retrieve the 'view' from the container
return new HomeController($view);
};
$app->get('/new', [HomeController::class, 'home');
// or $app->get('/new', HomeController::class . ':home');
另一答案
用Pheara的答案我得到另一个错误,是下一个:
Type: Error
Message: Undefined constant 'Appcontrollers'
File:/Users/alfonso/Sites/powertv_api/src/Controllers/HomeController.php
Line: 5
错误的痕迹是下一个:
/Users/alfonso/Sites/powertv_api/vendor/composer/ClassLoader.php(444): include()
#1 /Users/alfonso/Sites/powertv_api/vendor/composer/ClassLoader.php(322): ComposerAutoloadincludeFile('/Users/alfonso/...')
#2 [internal function]: ComposerAutoloadClassLoader- >loadClass('App\Controllers...')
#3 [internal function]: spl_autoload_call('App\Controllers...')
#4 /Users/alfonso/Sites/powertv_api/vendor/slim/slim/Slim/CallableResolver.php(89): class_exists('App\Controllers...')
#5 /Users/alfonso/Sites/powertv_api/vendor/slim/slim/Slim/CallableResolver.php(67): SlimCallableResolver->resolveCallable('App\Controllers...')
#6 /Users/alfonso/Sites/powertv_api/vendor/slim/slim/Slim/CallableResolverAwareTrait.php(45): SlimCallableResolver->resolve('App\Controllers...')
#7 /Users/alfonso/Sites/powertv_api/vendor/slim/slim/Slim/Route.php(330): SlimRoutable->resolveCallable('App\Controllers...')
#8 /Users/alfonso/Sites/powertv_api/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(117): SlimRoute->__invoke(Object(SlimHttpRequest), Object(SlimHttpResponse))
#9 /Users/alfonso/Sites/powertv_api/vendor/slim/slim/Slim/Route.php(313): SlimRoute->callMiddlewareStack(Object(SlimHttpRequest), Object(SlimHttpResponse))
#10 /Users/alfonso/Sites/powertv_api/vendor/slim/slim/Slim/App.php(495): SlimRoute->run(Object(SlimHttpRequest), Object(SlimHttpResponse))
#11 /Users/alfonso/Sites/powertv_api/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(117): SlimApp->__invoke(Object(SlimHttpRequest), Object(SlimHttpResponse))
#12 /Users/alfonso/Sites/powertv_api/vendor/slim/slim/Slim/App.php(388): SlimApp->callMiddlewareStack(Object(SlimHttpRequest), Object(SlimHttpResponse))
#13 /Users/alfonso/Sites/powertv_api/vendor/slim/slim/Slim/App.php(296): SlimApp->process(Object(SlimHttpRequest), Object(SlimHttpResponse))
#14 /Users/alfonso/Sites/powertv_api/public/index.php(39): SlimApp->run()
#15 {main}
要解决此错误,您必须将命名空间放在3行中:
<?php
namespace [yournamespace]
//rest of your controller
以上是关于Slim框架消息:Callable Homecontroller不存在的主要内容,如果未能解决你的问题,请参考以下文章