実現したいこと
- PHP の Slim 上で DI を利用したい
- SampleInterface と SampleClass は require なしで利用したい
前提
PHP と Slim を用いて DI を試そうとしていたところ、
名前解決に失敗しているかのようなエラーが発生しました。
発生している問題・エラーメッセージ
[Sun Jun 25 14:32:55 2023] [::1]:51400 [200]: GET /sample - Uncaught DI\Definition\Exception\InvalidDefinition: Entry "MySpace\SampleInterface" cannot be resolved: the class doesn't exist Full definition: Object ( class = #UNKNOWN# MySpace\SampleClass lazy = false ) in /Users/sutonea/dev/php/hello_slim/vendor/php-di/php-di/src/Definition/Exception/InvalidDefinition.php:19 Stack trace: #0 /Users/sutonea/dev/php/hello_slim/vendor/php-di/php-di/src/Definition/Resolver/ObjectCreator.php(103): DI\Definition\Exception\InvalidDefinition::create(Object(DI\Definition\AutowireDefinition), 'Entry "MySpace\\...') #1 /Users/sutonea/dev/php/hello_slim/vendor/php-di/php-di/src/Definition/Resolver/ObjectCreator.php(56): DI\Definition\Resolver\ObjectCreator->createInstance(Object(DI\Definition\AutowireDefinition), Array) #2 /Users/sutonea/dev/php/hello_slim/vendor/php-di/php-di/src/Definition/Resolver/ResolverDispatcher.php(60): DI\Definition\Resolver\ObjectCreator->resolve(Object(DI\Definition\AutowireDefinition), Array) #3 /Users/sutonea/dev/php/hello_slim/vendor/php-di/php-di/src/Container.php(353): DI\Definition\Resolver\ResolverDispatcher->resolve(Object(DI\Definition\AutowireDefinition), Array) #4 /Users/sutonea/dev/php/hello_slim/vendor/php-di/php-di/src/Container.php(136): DI\Container->resolveDefinition(Object(DI\Definition\AutowireDefinition)) #5 /Users/sutonea/dev/php/hello_slim/public/index.php(30): DI\Container->get('MySpace\\SampleI...') #6 /Users/sutonea/dev/php/hello_slim/vendor/slim/slim/Slim/Handlers/Strategies/RequestResponse.php(38): {closure}(Object(Slim\Psr7\Request), Object(Slim\Psr7\Response), Array) #7 /Users/sutonea/dev/php/hello_slim/vendor/slim/slim/Slim/Routing/Route.php(358): Slim\Handlers\Strategies\RequestResponse->__invoke(Object(Closure), Object(Slim\Psr7\Request), Object(Slim\Psr7\Response), Array) #8 /Users/sutonea/dev/php/hello_slim/vendor/slim/slim/Slim/MiddlewareDispatcher.php(65): Slim\Routing\Route->handle(Object(Slim\Psr7\Request)) #9 /Users/sutonea/dev/php/hello_slim/vendor/slim/slim/Slim/MiddlewareDispatcher.php(65): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request)) #10 /Users/sutonea/dev/php/hello_slim/vendor/slim/slim/Slim/Routing/Route.php(315): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request)) #11 /Users/sutonea/dev/php/hello_slim/vendor/slim/slim/Slim/Routing/RouteRunner.php(68): Slim\Routing\Route->run(Object(Slim\Psr7\Request)) #12 /Users/sutonea/dev/php/hello_slim/vendor/slim/slim/Slim/MiddlewareDispatcher.php(65): Slim\Routing\RouteRunner->handle(Object(Slim\Psr7\Request)) #13 /Users/sutonea/dev/php/hello_slim/vendor/slim/slim/Slim/App.php(199): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request)) #14 /Users/sutonea/dev/php/hello_slim/vendor/slim/slim/Slim/App.php(183): Slim\App->handle(Object(Slim\Psr7\Request)) #15 /Users/sutonea/dev/php/hello_slim/public/index.php(35): Slim\App->run() #16 {main} thrown in /Users/sutonea/dev/php/hello_slim/vendor/php-di/php-di/src/Definition/Exception/InvalidDefinition.php on line 19 [Sun Jun 25 14:32:55 2023] [::1]:51400 Closing
該当のソースコード
public/index.php
php
1<?php 2 3// この2行がない状態で実行できるようにしたい 4// require_once __DIR__ . '/../src/MySpace/SampleInterface.php'; 5// require_once __DIR__ . '/../src/MySpace/SampleClass.php'; 6 7use DI\ContainerBuilder; 8use Psr\Http\Message\ResponseInterface as Response; 9use Psr\Http\Message\ServerRequestInterface as Request; 10use Slim\Factory\AppFactory; 11use MySpace\SampleClass as SampleClass; 12use MySpace\SampleInterface as SampleInterface; 13 14require __DIR__ . '/../vendor/autoload.php'; 15 16$containerBuilder = new ContainerBuilder(); 17$containerBuilder->addDefinitions([ 18 SampleInterface::class => \DI\autowire(SampleClass::class), 19]); 20$container = $containerBuilder->build(); 21 22$app = AppFactory::create(); 23 24$app->get('/', function (Request $request, Response $response, $args) { 25 $response->getBody()->write("Hello world!"); 26 return $response; 27}); 28 29$app->get('/sample', function (Request $request, Response $response, $args) use ($container) { 30 $klass = $container->get(SampleInterface::class); 31 $response->getBody()->write($klass->execute()); 32 return $response; 33}); 34 35$app->run(); 36
src/MySpace/SampleInterface.php
php
1<?php 2namespace MySpace; 3 4interface SampleInterface { 5 public function execute(): string; 6} 7?> 8
src/MySpace/SampleClass.php
php
1<?php 2namespace MySpace; 3 4class SampleClass implements SampleInterface { 5 public function execute(): string { 6 return "Sample"; 7 } 8} 9?> 10
composer.json
json
1{ 2 "require": { 3 "slim/slim": "4.*", 4 "slim/psr7": "^1.6", 5 "php-di/php-di": "^7.0" 6 }, 7 "autoload": { 8 "psr-4": { 9 "MySpace\\": "src/" 10 } 11 } 12}
試したこと
composer dump-autoload
の実行- サーバー再起動
補足情報(FW/ツールのバージョンなど)
OS : Mac OS 12.6.5
PHP 8.2.7
Slim: 4.11.0
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/06/25 10:50