简体   繁体   English

PHP-DI调用默认参数为null的方法

[英]PHP-DI Call a method which have default parameter to null

I'm trying to use a PHP-DI Call on a method which have a default parameter but i get this error 我正在尝试对具有默认参数的方法使用PHP-DI调用 ,但出现此错误

Fatal error: Uncaught Invoker\\Exception\\NotEnoughParametersException: Unable to invoke the callable because no value was given for parameter 1... 致命错误:未捕获的Invoker \\ Exception \\ NotEnoughParametersException:无法调用可调用对象,因为没有为参数1指定任何值...

PS : PHP-DI 6 PS:PHP-DI 6

  Classe Bill
  {

  public function index($slug=null,Request $request){
            //----
       }
  }

use DI\ContainerBuilder;

$containerBuilder = new ContainerBuilder;
$container = $containerBuilder->build();

$controller = 'Bill' ;
$method = 'index';
$parameters = []; 
$response = $container->call([$controller,$method], $parameters);

Works: 作品:

class TestController { function doAction(Request $request, int $id = null) {} }

Does not work: 不起作用:

class TestController { function doAction(int $id = null, Request $request) {} }

Source: https://github.com/PHP-DI/Slim-Bridge/issues/37#issuecomment-368954250 来源: https : //github.com/PHP-DI/Slim-Bridge/issues/37#issuecomment-368954250

You need to provide a value for the parameter $slug . 您需要为参数$slug提供一个值。

When a parameter is optional before non-optional parameters it cannot be omitted. 如果参数在非可选参数之前是可选的,则不能忽略它。 So you have to provide its value. 因此,您必须提供其价值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM