美文网首页PHP经验分享PHP实战
使用composer搭建自己的项目!【二】

使用composer搭建自己的项目!【二】

作者: 秋名山吴师傅 | 来源:发表于2018-12-01 14:50 被阅读2次

    仿照Laravel的Request

    [前言]

    我们在上一章已经能把项目通过composer上fast-router产生对应关系,我们这一章继续优化我们的项目。

    /**
     *  分发路由
     */
    public static function Route()
    {
    
        if (self::$routeInfo[0] !=0){
            //把方法、控制器根据@符号炸开
            $routerMessage = explode('@', self::$routeInfo[1]);
            //由于我们控制都是在app/的Controller里面我们这里为什么可以大写由于自动加载做了对应关系
            $controller = 'App\\Controller\\' . $routerMessage[0];
            $controller = str_replace('/','\\',$controller);
            $action = $routerMessage[1];
            $obj = new $controller;
            self::$htmlresult=$obj->$action();
        }else{
            throw new ErrorException('路由错误!请检查路由是否正确!');
        }
    }
    public static function send()
    {
        $res = self::$htmlresult;
    
        
        if (gettype($res) == 'string') {
            //字符串
            echo $res;
        } else {
    
            echo json_encode($res);
        }
    }
    

    上面使用了$htmlresult静态变量装数据,在send方法输出,在判断如果不是字符串,是数组、对象会自动把数据转换成json并返回。


    我们在使用laravel发现这个框架里逻辑函数中,存在一些有时候会有Request $request的情况,有时候又可以没有,怎么实现?
    我在翻阅了google,baidu都搜寻过都没有解决的办法,怎么这个那么有意思的东西都没人有好奇心呢?难道每个人都知道吗?
    我们在本项目中Request类就是用来过滤一些post、get一些不安全的数据。
    于是乎我在上php官方手册查阅的时候想到一个可行的办法解决这个问题——反射

    php手册

    我们动手开始操作。

    public static function Route()
    {
    
        if (self::$routeInfo[0] !=0){
            $routerMessage = explode('@', self::$routeInfo[1]);
            $controller = 'App\\Controller\\' . $routerMessage[0];
            $controller = str_replace('/','\\',$controller);
            $action = $routerMessage[1];
            $obj = new $controller;
            //通过反射获得参数
            $reflection = new \ReflectionMethod($controller, $action);
            $actionParameters=$reflection->getParameters();
    
            //获取到方法参数为一个类
    
            if (!empty($actionParameters)){
                //如果参数不为空
                foreach ($actionParameters as $actionP){
                    $parame=$actionP->getType()->getName();
                }
                $parameters=new $parame;
                self::$htmlresult=$obj->$action($parameters);
            }else{
                //如果参数为空
                self::$htmlresult = $obj->$action();
            }
    
    
    
        }else{
            throw new ErrorException('路由错误!请检查路由是否正确!');
        }
    }
    

    我们在libs类库中创建Request.php类

    <?php
    /**
     *
     * Request.php
     * User: kalvin
     * Date: 2018/2/5
     * Time: 下午4:49
     */
    
    namespace libs;
    
    
    class Request
    {
        public function get($getkey){
            echo $getkey;
        }
    }
    

    IndexController中的showIndex方法我们可以试下无论有无Request参数都可正确使用

    <?php
    /**
     *
     * IndexController.php
     * User: kalvin
     * Date: 2018/2/5
     * Time: 下午4:22
     */
    
    namespace App\Controller\Back\View;
    
    
    use libs\Request;
    
    class IndexController
    {
        public function showIndex(Request $request)
        {
            return '你好';
        }
    }
    

    如果觉得我的文章对你有帮助又或者喜欢,别忘了关注.喜欢加转发,当然如果可以打赏我一下奶茶钱也是可以的哦(#.#)

    相关文章

      网友评论

        本文标题:使用composer搭建自己的项目!【二】

        本文链接:https://www.haomeiwen.com/subject/ooxrcqtx.html