Laravel5.5 controller parameter passing order problem and solution

*文
Release: 2023-03-18 09:08:02
Original
2288 people have browsed it

The controller of laravel5.5 provides the ability to automatically inject based on the method parameter type. But sometimes there is a slight inconvenience, which is reflected in the fact that the injection of method parameters is not entirely based on the parameter name. If the order of the incoming parameters is changed, it will cause a type mismatch error. This article will solve the problem from an in-depth analysis of its injection principle.

1. Controller method parameter injection step design

1. Add routing in /routes/web.php

Route::get('/diary/show/{diary}/{page?}','Diary\DiaryController@list');
Copy after login

2. Write the controller file DiaryController.php and put it in Go to the path of /app/Http/Controllers/Diary/

<?php
namespace App\Http\Controllers\Diary;
use App\Http\Controllers\Controller;
class DiaryController extends Controller
{
    public function show(\App\Diary $diary,$page=11){
        var_dump($diary->title,$page); 
  } 
}
Copy after login

3. Build the model\App\Diary and install it into the database (omitted)

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Diary extends Model
{
    protected $table=&#39;diary&#39;;
    public $timestamps = false;
}
Copy after login

4. Access the controller method

Open the browser and enter: "http://127.0.0.1//diary/show/4/12"

At this time, the title field value and 12## of id=4 in the data table diary are output.

#2. Injection parameter type description

Description: Laravel will generate and inject instance objects based on the matching {diary} and {page} variables in the request route and the method parameter types required in the controller method. In the controller method,

There are three situations for different parameter types:

1. If the parameter type implements the UrlRoutable interface (that is, inherited from Illuminate\Database\Eloquent\Model), Then search the table corresponding to the model object for the record whose id value is the matching parameter value in the route, and build the model object;

2. If the parameter type is a custom type (the UrlRoutable interface is not implemented), then Laravel will construct an object and then inject it;

3. If the parameter type is a basic data type and the name is the name defined in the routing parameter, the value will be obtained from the routing parameter;

4. If the parameter type is a basic data type but the name is not defined in the routing parameter, use the default value if there is one, otherwise the system will prompt an error.

3. Analysis of current problems with injected parameters

Referring to Java's Spring MVC framework, laravel's parameter type injection still has flaws, mainly reflected in the fact that it is not injected entirely according to the parameter name.

1. If you change the order of controller parameters, a parameter type transfer error will occur. If you change the order of the parameters of the show method controlled by DiaryController, an error will occur:

<?php
namespace App\Http\Controllers\Diary;
use App\Http\Controllers\Controller;
class DiaryController extends Controller
{
    public function show($page,\App\Diary $diary){
        var_dump($diary->title,$page); 
  } 
}
Copy after login

2. Since the parameter type is a basic data type (see 2 (3)), it is not a parameter injected according to the name. Therefore, changing the code to the following will also run normally

<?php
namespace App\Http\Controllers\Diary;
use App\Http\Controllers\Controller;
class DiaryController extends Controller
{
    public function show(\App\Diary $diary,$pag){
        var_dump($diary->title,$pag); 
  } 
}
Copy after login

4. laravel5.5 controller method Parameter injection source code analysis

1. The parameter type that implements the UrlRoutable interface is constructed by the routing middleware Illuminate\Routing\Middleware\SubstituteBinding

    public function handle($request, Closure $next)
    {
        $this->router->substituteBindings($route = $request->route());
        $this->router->substituteImplicitBindings($route);
        return $next($request);
    }
Copy after login

Illuminate\Routing\Router's substituteImplicitBindings method

    public function substituteImplicitBindings($route)
    {
        ImplicitRouteBinding::resolveForRoute($this->container, $route);
    }
Copy after login

Implemented in the resolveForRoute method of Illuminate\Routing\ImplicitRouteBinding

    public static function resolveForRoute($container, $route)
    {
        //从路由参数中获取参数值,$parameters为 [&#39;diary&#39;:&#39;4&#39;,&#39;page&#39;:&#39;12&#39;]
        $parameters = $route->parameters();
        //获取控制器的函数参数列表,此处传入UrlRoutable::class,只返回实现UrlRoutable接口的参数
        $signatureParameters = $route->signatureParameters(UrlRoutable::class);
        foreach ($signatureParameters as $parameter) {
            if (! $parameterName = static::getParameterName($parameter->name, $parameters)) {
                continue;
            }
            $parameterValue = $parameters[$parameterName];
            if ($parameterValue instanceof UrlRoutable) {
                continue;
            }
            //构建模型的实例(基础自Illuminate\Database\Eloquent\Model),此处为App\Diary
            $instance = $container->make($parameter->getClass()->name);
            //将参数值绑定到模型,参加Illuminate\Database\Eloquent\Model的resolveRouteBinding方法
            if (! $model = $instance->resolveRouteBinding($parameterValue)) {
                throw (new ModelNotFoundException)->setModel(get_class($instance));
            }
       //根据参数名称注入模型实例
            $route->setParameter($parameterName, $model);
        }
    }
Copy after login

Additional instructions:

The setParameter method of the $route object (Illuminate\Routing\Route type) is called here to illustrate the model The parameter type (see 2 (1)) is injected into the model instance by matching the parameter type and parameter name at the same time

2. Other types of controller parameters are bound when running the routing controller

The key part is implemented in the dispatch method of Illuminate\Routing\ControllerDispatcher:

    public function dispatch(Route $route, $controller, $method)
    {
        //解析控制器方法的参数
        $parameters = $this->resolveClassMethodDependencies(
            $route->parametersWithoutNulls(), $controller, $method
        );
        if (method_exists($controller, &#39;callAction&#39;)) {
            //通过Illuminate\Routing\Controller的callAction调用控制器方法
            return $controller->callAction($method, $parameters);
        }
        //直接调用控制器方法
        return $controller->{$method}(...array_values($parameters));
    }
Copy after login

Call the resolveClassMethodDependencies method

  public function resolveMethodDependencies(array $parameters, ReflectionFunctionAbstract $reflector)
    {
        $instanceCount = 0;
        $values = array_values($parameters);
        //通过方法反射获取方法参数
        foreach ($reflector->getParameters() as $key => $parameter) {
        //如果有默认值则返回默认值,如果是自定义方法则构建实例返回
            $instance = $this->transformDependency(
                $parameter, $parameters
            );
            if (! is_null($instance)) {
                $instanceCount++;
                //自定义类型(未实现UrlRoutable接口)的实例注入
                $this->spliceIntoParameters($parameters, $key, $instance);
            } elseif (! isset($values[$key - $instanceCount]) &&
                      $parameter->isDefaultValueAvailable()) {
                //未在路由参数中定义,但有默认值的参数注入
                $this->spliceIntoParameters($parameters, $key, $parameter->getDefaultValue());
            }
        }
        return $parameters;
    }
Copy after login

Summary of the problem:

1. Model parameters (see 2 ( 1)) and the basic types whose names are defined in the routing parameters (see 2 (3)) must first be passed into the controller method in the order defined in the routing, otherwise a type mismatch error will occur;

2. Custom types (see 2 (2)) and basic type parameters whose names are not defined in routing parameters (see 2 (4)) are passed in in the order of appearance in the controller method.

5. Problem fix

Open the /vendor/laravel/framework/src/Illuminate/Routing/RouteDependencyResolverTrait.php file,

Modify the resolveMethodDependencies method to the following code

    public function resolveMethodDependencies(array $parameters,ReflectionFunctionAbstract $reflector){
        $methodParameters=[];
        foreach($reflector->getParameters() as $key=>$parameter){
            $name=$parameter->getName();
            $instance=$this->transformDependency($parameter, $parameters);
            if(!is_null($instance)){
                $methodParameters[]=$instance;
            }elseif(!isset($parameters[$name]) && $parameter->isDefaultValueAvailable()){
                $methodParameters[]=$parameter->getDefaultValue();
            }else{
                $methodParameters[]=isset($parameters[$name]) ? $parameters[$name] : null;
            }
        }
        return $methodParameters;
    }
Copy after login
After the modification, the controller method parameters are injected completely according to the name and type. The code becomes more concise and the function is more powerful!

If the parameter is not defined in the route and no default value is provided, null will be injected.

Related recommendations:

Detailed explanation of PHP's method of automatic dependency injection based on reflection mechanism

What is dependency injection?

How to use the corresponding interface of Laravel 5.5?

The above is the detailed content of Laravel5.5 controller parameter passing order problem and solution. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template