Slim 框架消息:Callable Homecontroller 不存在 [英] Slim framework Message: Callable Homecontroller does not exist
问题描述
public/index.php
run();
<小时>
这里有 composer.json
<代码>{自动加载":{psr-4":{应用程序\\":源代码"}},要求": {"php": ">=5.5.0","slim/slim": "^3.1","slim/php-view": "^2.0","独白/独白": "^1.17",照亮/数据库":~5.1","slim/twig-view": "^2.3"},需要开发":{"phpunit/phpunit": ">=4.8 < 6.0"},自动加载开发":{psr-4":{"测试\\": "测试/"}},配置":{进程超时":0},}
<小时>
这是我的控制器,我得到了命名空间错误,并且在我看到的所有示例中都使用了它,所以..我不知道该怎么做
container = $container;}公共函数家($request,$response,$args){echo "locura";//你的代码//访问容器中的项目... $this->container->get('');返回 $response;}公共函数联系($request,$response,$args){//你的代码//访问容器中的项目... $this->container->get('');返回 $response;}}
<小时>
实际配置在我的 routes.php 中,我已经把这个:
$app->get('/new', \Homecontroller::class . ':home');
<小时>
配置前
在上面的代码中,我尝试将控制器创建到容器中,然后使用
$app->get('/new', '\HomeController:home');
在dependencies.php中我放了这段代码:
$container['HomeController'] = function($c) {$view = $c->get("view");//从容器中检索视图"返回新的 HomeController($view);
};
但是我没有得到任何配置的结果
<小时>我想从路由器加载 HomeController
这是我在输入 api.powertv/new
时遇到的错误 类型:RuntimeException消息:Callable Homecontroller 不存在文件:/Users/alfonso/Sites/powertv_api/vendor/slim/slim/Slim/CallableResolver.php线路:90
我来了,我把这篇文章当作我的最后一个资源,如果失败了我不知道我要做什么.
尝试在您的:
dependencies.php
<前>$container['yourController'] = function ($c) {$view = $c['view'];返回新的 \App\Controllers\YourController($view);}routes.php
<前>$app->get('your_defined_route_name', \App\Controllers\YourController::class . ':YourControllerMethod');
public/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 \Slim\App($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();
Here we have the 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
},
}
Here is my controller, i obtain a error with the namespace and in all the examples that i have seen use this so.. I don't know what to do more
<?php
namespace \App\Controllers;
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;
}
}
Actual configuration In my routes.php I have put this:
$app->get('/new', \Homecontroller::class . ':home');
Before configuration
In the above code, I tried to create the controller into the container, and then use
$app->get('/new', '\HomeController:home');
And in the dependencies.php I put this code:
$container['HomeController'] = function($c) {
$view = $c->get("view"); // retrieve the 'view' from the container
return new HomeController($view);
};
but I didn't obtain any result with any configuration
I would like to load the HomeController from router
This is the error that I have when I put 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
i come here, and I put this post like my last resource, if this fails I don't know what I am going to do.
Try to use absolute namespace path in your:
dependencies.php
$container['yourController'] = function ($c) { $view = $c['view']; return new \App\Controllers\YourController($view); }
routes.php
$app->get('your_defined_route_name', \App\Controllers\YourController::class . ':YourControllerMethod');
这篇关于Slim 框架消息:Callable Homecontroller 不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!