Laravel 9: 可选控制器参数错误

huangapple go评论52阅读模式
英文:

Laravel 9: optional controller parameter error

问题

So the $id is an optional parameter, but it gives me the following error:

所以 $id 是一个可选参数,但它给了我以下错误:

App\Http\Controllers\UserController::getSoleUserProfile(): Argument #1 ($id) must be of type ?int, App\Domain\User\UserService given

如果我将其设置为必须的,它就正常工作,我做错了什么吗?

P.S. UserService is an implementation of UserInterface

附注:UserService 是 UserInterface 的一个实现。

英文:

Consider the following example:

Route::get('/users/profile/{id?}', [UserController::class, 'getSoleUserProfile']);


class UserController extends Controller
{

    public function getSoleUserProfile(
        ?int $id = null,
        UserInterface $users,
    ) {
        ...
    }
}

So the $id is an optional parameter, but it gives me the following error:

App\\Http\\Controllers\\UserController::getSoleUserProfile(): Argument #1 ($id) must be of type ?int, App\\Domain\\User\\UserService given

If I make it required it works good, am I doing something wrong?

P.S. UserService is an implementation of UserInterface

答案1

得分: 1

当你想传递一个可选变量时,需要将可选变量放在你想调用的控制器方法的最后一个参数中。

由于你可能还想将请求传递给控制器,以下代码可能会帮助你实现你想要的目标。

public function getSoleUserProfile(
     Request $request,
     UserService $users,
     ?int $id = null
 ) {
        ...
   }
英文:

when you want to pass an optional variable, you need to have the optional variable in the last parameter of the controller method you want to call.

since you will probably want to pass the request to the controller as well, the following code could help you to get what you want to achieve.

public function getSoleUserProfile(
     Request $request,
     UserService $users,
     ?int $id = null
 ) {
        ...
   }

答案2

得分: 0

你收到的错误信息表明,你传递给getSoleUserProfile()方法的第二个参数,UserInterface $users,被解释为第一个参数而不是可选的$id参数。这表明$id参数可能没有获得预期的值或类型。

为解决这个问题,你可以尝试调整getSoleUserProfile()方法中参数的顺序,将$id放在第一个参数,然后是$users

public function getSoleUserProfile(
    int|null $id = null,
    UserInterface $users
) {
    // ...
}

请注意,我还将$id的类型声明更改为使用int|null类型,而不是?int。这两种写法是等效的,但前者是更常用的语法。

通过进行这些更改,你应该能够调用该方法,传递$id值或null值,并且$users参数应该被正确解释为第二个参数。

如果仍然遇到问题,你可能需要检查UserInterface类的实现,确保它与$users参数的类型提示兼容。

英文:

The error message you received indicates that the second parameter you passed to the getSoleUserProfile() method, UserInterface $users, is being interpreted as the first argument instead of the optional $id parameter. This suggests that the $id parameter may not be receiving the expected value or type.

To resolve this issue, you could try adjusting the order of the parameters in the getSoleUserProfile() method so that $id is the first parameter, followed by $users:

public function getSoleUserProfile(
    int|null $id = null,
    UserInterface $users
) {
    // ...
}

Note that I've also changed the type declaration for $id to use the int|null type instead of ?int. Both are equivalent, but the former is the more commonly used syntax.

By making this change, you should be able to call the method with either an $id value or a null value, and the $users parameter should be correctly interpreted as the second parameter.

If you still encounter issues, you may want to check the implementation of the UserInterface class to ensure that it is compatible with the type hint for the $users parameter.

huangapple
  • 本文由 发表于 2023年4月6日 19:29:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定