Slim framework Message: Callable Homecontroller does not exist

感情迁移 提交于 2020-01-05 07:47:12

问题


Project Structure


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.


回答1:


Try to use absolute namespace path in your:

  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');
    



回答2:


The name inside the container and thoose who you use in the route must match You've been set the container key to HomeController and used \HomeController in the route, these strings are not equal.

either self create the strings

$container['HomeController'] = function($c) {
    $view = $c->get("view"); // retrieve the 'view' from the container
    return new HomeController($view);
};
$app->get('/new', 'HomeController:home');

or use the ::class syntax for both:

$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');



回答3:


with the answer of Pheara I obtain another error, is the next:

Type: Error
Message: Undefined constant 'App\controllers'
File:/Users/alfonso/Sites/powertv_api/src/Controllers/HomeController.php
Line: 5

the trace of the error is the next:

 /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}

For solve this error you have to put the namespace in the 3 line:

<?php 

namespace [yournamespace]

//rest of your controller


来源:https://stackoverflow.com/questions/47724219/slim-framework-message-callable-homecontroller-does-not-exist

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!