2016-06-19 14 views
5

मैं स्लिम में नियंत्रक का उपयोग करने के लिए हालांकि त्रुटिस्लिम नियंत्रक मुद्दा: ContainerInterface, स्लिम \ कंटेनर का उदाहरण दिया का एक उदाहरण होना चाहिए

पीएचपी Catchable घातक त्रुटि मिलती रहती है कोशिश कर रहा हूँ: तर्क 1
TopPageController के लिए पारित :: __ निर्माण() ContainerInterface का एक उदाहरण,
स्लिम \ कंटेनर का उदाहरण दिया

मेरे index.php होना चाहिए

<?php 
use \Psr\Http\Message\ServerRequestInterface as Request; 
use \Psr\Http\Message\ResponseInterface as Response; 

require '../vendor/autoload.php'; 
require 'settings.php'; 

spl_autoload_register(function ($classname) { 
    require ("../classes/" . $classname . ".php"); 
}); 

$app = new \Slim\App(["settings" => $config]); 
$app->get('/', function (Request $request, Response $response) { 
    $response->getBody()->write("Welcome"); 
    return $response; 
}); 
$app->get('/method1', '\TopPageController:method1'); 
$app->run(); 
?> 

मेरा टॉप पेज कंट्रोलर.एफपी

<?php 
class TopPageController { 
    protected $ci; 
    //Constructor 
    public function __construct(ContainerInterface $ci) { 
     $this->ci = $ci; 
    } 

    public function method1($request, $response, $args) { 
     //your code 
     //to access items in the container... $this->ci->get(''); 
     $response->getBody()->write("Welcome1"); 
     return $response; 
    } 

    public function method2($request, $response, $args) { 
     //your code 
     //to access items in the container... $this->ci->get(''); 
     $response->getBody()->write("Welcome2"); 
     return $response; 
    } 

    public function method3($request, $response, $args) { 
     //your code 
     //to access items in the container... $this->ci->get(''); 
     $response->getBody()->write("Welcome3"); 
     return $response; 
    } 
} 
?> 

धन्यवाद। मैं स्लिम 3 का उपयोग कर रहा हूं।

+1

आपके पास कंटेनर इंटरफेस पर उचित नामस्थान नहीं है। यह 'इंटरऑप \ कंटेनर \ कंटेनरइटरफेस' होना चाहिए – geggleto

उत्तर

12

आपका कोड स्लिम 3 दस्तावेज पर http://www.slimframework.com/docs/objects/router.html पर आधारित प्रतीत होता है जिसमें अपवाद को फेंकने से बचने के लिए सभी आवश्यक कोड शामिल नहीं हैं।

आपके पास मूल रूप से काम करने के लिए दो विकल्प हैं।

विकल्प 1:

आयात अपने index.php में नाम स्थान, जैसे यह Request और Response के लिए किया जा रहा है:

use \Interop\Container\ContainerInterface as ContainerInterface; 

विकल्प 2:

बदलें निर्माता TopPageController के लिए:

public function __construct(Interop\Container\ContainerInterface $ci) { 
    $this->ci = $ci; 
} 

टी एल; डॉ

कारण अपवाद फेंक दिया जाता है है कि Slim\Container वर्ग Interop\Container\ContainerInterface इंटरफ़ेस का उपयोग कर रहा है (स्रोत कोड देखें):

use Interop\Container\ContainerInterface; 

के बाद से Slim\Container है Pimple\Container का विस्तार, निम्नलिखित आपके नियंत्रक की विधि के लिए वैध (काम कर रहे) प्रकार की घोषणाएं होनी चाहिए:

public function __construct(Pimple\Container $ci) { 
    $this->ci = $ci; 
} 

... या यहाँ तक कि ...

public function __construct(ArrayAccess $ci) { 
    $this->ci = $ci; 
} 
0

बस पेस्ट अपने नियंत्रक में कोड नीचे

use \Psr\Http\Message\ServerRequestInterface as Request; 
use \Psr\Http\Message\ResponseInterface as Response; 
use \Interop\Container\ContainerInterface as ContainerInterface; 

आपका नियंत्रक के निर्माण की तरह लग रही किया जाना चाहिए नीचे

public function __construct(ContainerInterface $container) { 
     parent::__construct($container); 

    } 

मुझे लगता है कि आप नाम देने में गलती कर रहे हैं कंटेनर इंटरफेस के लिए नियंत्रक में अंतरिक्ष।

संबंधित मुद्दे