About the principle of action parameter binding in some PHP frameworks

WBOY
Release: 2016-07-06 13:53:58
Original
1052 people have browsed it

For example THINKPHP

<code>class xxController
{
    public function index($id,$age)
    {
        //这些参数是通过$_GET['id'],$_GET['age']来的
        
    }
}
//按PHP的实现方法大致应该是以下
$ctr = new xxController();
return $ctr->index(); //他这里怎么有知道应该传哪些参数呢?!

//本人愚钝,我想不是这样做的,那应该是怎么实现呢?!!请各位大神讲解一下其中的实现原理!</code>
Copy after login
Copy after login

Reply content:

For example THINKPHP

<code>class xxController
{
    public function index($id,$age)
    {
        //这些参数是通过$_GET['id'],$_GET['age']来的
        
    }
}
//按PHP的实现方法大致应该是以下
$ctr = new xxController();
return $ctr->index(); //他这里怎么有知道应该传哪些参数呢?!

//本人愚钝,我想不是这样做的,那应该是怎么实现呢?!!请各位大神讲解一下其中的实现原理!</code>
Copy after login
Copy after login

Reflection, the parameter list of the class method obtained through reflection. Then just match it up.

<code><?php
/**
 * 一个用于测试的类
 * Class TestController
 */
class TestController {
    /**
     * 嗯,著名的hello world
     * @param $name
     */
    public function helloWordAction($name)
    {
        echo "hello {$name}!".PHP_EOL;
    }
}

# 通过反射进行参数绑定调起类的方法
# @see http://php.net/manual/zh/book.reflection.php

# 方法,从路由获取的,类也是由路由获取的,这里意思一下就好了
$action = 'helloWordAction';
# 传进来的参数,从路由获取的
$paramsInput['name'] = 'toozy';

# 获取类的反射
$controllerReflection = new ReflectionClass(TestController::class);
# 不能实例化,就是不能new一个的话,这个游戏就玩不下去了啊
if (!$controllerReflection->isInstantiable()) {
    throw new RuntimeException("{$controllerReflection->getName()}不能被实例化");
}

# 获取对应方法的反射
if (!$controllerReflection->hasMethod($action)) {
    throw new RuntimeException("{$controllerReflection->getName()}没有指定的方法:{$action}");
}
$actionReflection = $controllerReflection->getMethod($action);
# 获取方法的参数的反射列表(多个参数反射组成的数组)
$paramReflectionList = $actionReflection->getParameters();
# 参数,用于action
$params = [];
# 循环参数反射
# 如果存在路由参数的名称和参数的名称一致,就压进params里面
# 如果存在默认值,就将默认值压进params里面
# 如果。。。没有如果了,异常
foreach ($paramReflectionList as $paramReflection) {
    # 是否存在同名字的路由参数
    if (isset($paramsInput[$paramReflection->getName()])) {
        $params[] = $paramsInput[$paramReflection->getName()];
        continue;
    }
    # 是否存在默认值
    if ($paramReflection->isDefaultValueAvailable()) {
        $params[] = $paramReflection->getDefaultValue();
        continue;
    }
    # 异常
    throw new RuntimeException(
        "{$controllerReflection->getName()}::{$actionReflection->getName()}的参数{$paramReflection->getName()}必须传值"
    );
}

# 调起
$actionReflection->invokeArgs($controllerReflection->newInstance(), $params);</code>
Copy after login

Save as: reflection.php

<code>root@work:/media/sf_project/php-hello-world# php refrection.php 
hello toozy!</code>
Copy after login

The framework in this case has a "route definition", and the client can only match the defined route, which includes path, method, and request parameters.
Take Laravel as an example:

<code>Route::get('api/article/{articleId}', 'ArticleController@articleDetail');

//...

ArticleController extends Controller
{
    public function articleDetail($articleId)
    {
        //...
    }
}</code>
Copy after login

During the matching routing process, because the route defined contains the placeholder {articleId} in the agreed form, the routing module will match the URL containing articleId and obtain the parameter value and pass it to the action of the corresponding Controller.

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!