Slim框架消息:Callable Homecontroller不存在

时间:2017-12-09 00:54:50

标签: php routes slim

Project Structure

公开/ 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();

这里我们有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 \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;
    }
}

实际配置 在我的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

我来到这里,我把这篇文章写成我最后的资源,如果失败我不知道自己要做什么。

3 个答案:

答案 0 :(得分:0)

尝试在:

中使用绝对命名空间路径
  1. dependencies.php

     $container['yourController'] = function ($c) {
      $view = $c['view'];
      return new \App\Controllers\YourController($view);
    }
    
  2. routes.php文件

      $app->get('your_defined_route_name', \App\Controllers\YourController::class . ':YourControllerMethod');
    

答案 1 :(得分:0)

容器内的名称和您在路径中使用的人员必须匹配 您已将容器密钥设置为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');

答案 2 :(得分:0)

在Pheara的回答中我得到了另一个错误,是下一个错误:

Type: Error
Message: Undefined constant 'App\controllers'
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):   Composer\Autoload\includeFile('/Users/alfonso/...')
#2 [internal function]: Composer\Autoload\ClassLoader- >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): Slim\CallableResolver->resolveCallable('App\\Controllers...')
#6 /Users/alfonso/Sites/powertv_api/vendor/slim/slim/Slim/CallableResolverAwareTrait.php(45): Slim\CallableResolver->resolve('App\\Controllers...')
#7 /Users/alfonso/Sites/powertv_api/vendor/slim/slim/Slim/Route.php(330): Slim\Routable->resolveCallable('App\\Controllers...')
#8 /Users/alfonso/Sites/powertv_api/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(117): Slim\Route->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))
#9 /Users/alfonso/Sites/powertv_api/vendor/slim/slim/Slim/Route.php(313): Slim\Route->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#10 /Users/alfonso/Sites/powertv_api/vendor/slim/slim/Slim/App.php(495): Slim\Route->run(Object(Slim\Http\Request), Object(Slim\Http\Response))
#11 /Users/alfonso/Sites/powertv_api/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(117): Slim\App->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))
#12 /Users/alfonso/Sites/powertv_api/vendor/slim/slim/Slim/App.php(388): Slim\App->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#13 /Users/alfonso/Sites/powertv_api/vendor/slim/slim/Slim/App.php(296): Slim\App->process(Object(Slim\Http\Request), Object(Slim\Http\Response))
#14 /Users/alfonso/Sites/powertv_api/public/index.php(39): Slim\App->run()
#15 {main}

要解决此错误,您必须将命名空间放在第3行:

<?php 

namespace [yournamespace]

//rest of your controller