首页 > 技术 > 正文

yii框架路由的实现

标签:php

yii框架中路由的实现核心代码

<?php

if (PHP_SAPI != 'cli') {
    exit('Please run it in terminal!');
}
if ($argc < 3) {
    exit('At least 2 arguments needed!');
}
$controller = ucfirst($argv[1]) . 'Controller';
$action = 'action' . ucfirst($argv[2]);
// 检查类是否存在
if (!class_exists($controller)) {
    exit("Class $controller does not existed!");
}
// 获取类的反射
$reflector = new ReflectionClass($controller);
// 检查方法是否存在
if (!$reflector->hasMethod($action)) {
    exit("Method $action does not existed!");
}
// 取类的构造函数,返回的是ReflectionMethod对象
$constructor = $reflector->getConstructor();
// 取构造函数的参数
$parameters = $constructor->getParameters();
// 遍历参数
foreach ($parameters as $key => $parameter) {
    // 获取参数声明的类
    $injector = new ReflectionClass($parameter->getClass()->name);
    // 实例化参数声明类并填入参数列表
    $parameters[$key] = $injector->newInstance();
}
// 使用参数列表实例 controller 类
$instance = $reflector->newInstanceArgs($parameters);
// 执行
$instance->$action();

class HelloController
{
    private $model;
    public function __construct(TestModel $model)
    {
        $this->model = $model;
    }
    public function actionWorld()
    {
        echo $this->model->property, PHP_EOL;
    }
}
class TestModel
{
    public $property = 'property';
}


原创文章,转载请注明出处!
本文链接:https://wanghongjun2014.github.io/posts/yii-route.html
上篇: socket端口复用
下篇: 为什么socket server端需要bind, client端一般不需要bind

请修改_includes/comment.html添加评论代码。