Laravel 大致原理

<?php

interface IMessage{
    public function send();
}

class Message implements IMessage{
    public function send(){
        echo 'send';
    }
}

class ControllerA
{
    public function __construct()
    {
        echo '实例化';
    }
    public function index(IMessage $mes)
    {
        $mes->send();
    }
}

class Container{ //psr7好像定义了 容器规范

    private $arr=[];
    //\Closure
    public function bind($name, $fun)
    {
        $this->arr[$name]=$fun;
    }

    public function make($name)
    {
        $fun=$this->arr[$name];
        return call_user_func($fun);
    }
}

class Route{  //为了省事 本身 Route 使用魔术方法__callStatic实现 非静态方法
    private static $arr=[];
    public static function get($name,$controller)
    {
        self::$arr[$name]=$controller;
        self::invoke(); 
    }

    public static function  invoke()
    {
        global $container;
        $controller=self::$arr['callindex'];
        list($controllerClass,$method)=explode('@',$controller);
        //反射
        $reclass=new ReflectionClass($controllerClass);// 获取反射类
        $method=$reclass->getMethod($method);//index 方法
        $param=$method->getParameters(); //index 方法的所有参数
        $methodparamClassName=$param[0]->getClass()->getName(); //Imessage 第一个参数的 类名
        $paramInstance=$container->make($methodparamClassName);// 从容器中取这个类名绑定的实例

        $controllerInstance=$reclass->newInstance();//$a=new ControllerA //输出实例化
        $method->invoke($controllerInstance,$paramInstance);//执行$a->index  //输出  send

    }
}
$container=new Container();
$container->bind('IMessage',function(){
    return new Message();
});
Route::get('callindex','ControllerA@index'); //main start

/**
$reclass=new ReflectionClass('ControllerA');// 获取反射类
$method=$reclass->getMethod('index');//index 方法
$param=$method->getParameters(); //index 方法的所有参数
$methodparamClassName=$param[0]->getClass()->getName(); //Imessage 第一个参数的 类名
$paramInstance=$container->make('IMessage');// 从容器中取这个类名绑定的实例

$controllerInstance=$reclass->newInstance();//$a=new ControllerA
$method->invoke($controllerInstance,$paramInstance);//执行$a->index

**/
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 2

你这个代码稀烂的一笔,还发出来,还取个这个标题

7年前 评论

能说明问题就好!

6年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!