douli7841 2014-01-12 15:06
浏览 91
已采纳

Composer autoload无法正常工作

I'm using composer autoload for a mvc framework I've built. I've tested it and works on a Vagrant environment (config: http://pastebin.com/aAs2TFMh) and even on Windows.

I get this error: {"error":{"type":"Whoops\\Exception\\ErrorException","message":"Class 'Elysium\\Controllers\\Error' not found","file":"\/home\/glendme\/public_html\/clubsmade\/src\/elysium\/core\/Router.php","line":99}}

However when I deployed it on my vps with Ubuntu 13.04+php5.4 it started giving class not found errors. Same happened when I put it on a shared host.

I tried self-update, removing vendor dir and composer install again at no avail.

Here's my composer.json,

{
"name": "glend/elysium",
"description": "PHP MVC Framework.",
"version" : "0.1.0-dev",
"keywords" : ["mvc", "framework", "elysium", "glend"],
"homepage" : "http://mvc.blueberry.al",
"license" : "GPL-3.0+",
"authors": [
    {
        "name": "Glend Gjermeni",
        "email": "contact@glend.me",
        "homepage": "http://glend.me",
        "role": "Developer"
    }
],
"support": {
    "email": "support@blueberry.al"
},
"autoload": {
    "psr-0": {"Elysium": "src/"}
},
"require": {
    "filp/whoops": "1.*",
    "swiftmailer/swiftmailer": "*",
    "vlucas/valitron": "1.1.5",
    "ircmaxell/password-compat": "1.0.3"
}

}

And Router.php:

    <?php

namespace Elysium\Core;
use Elysium\Controllers;

/**
 * Manages all routing and URL requests for the framework.
 * @package Elysium\Core
 */
class Router
{
    private $url, $controller, $method, $params;
    private $allowedChars = array('-', '_', '/', '\\', '.');

    /**
     * Reads the passed URL from GET request into controller, method and params variables.
     */
    public function __construct()
    {
        if(!empty($_GET['page']))
        {
            if(ctype_alnum(str_replace($this->allowedChars, '', $_GET['page'])))
            {
                $this->url = $_GET['page'];
            }
            else
            {
                throw new Exception("Malformed URL");
            }
        }
        else
        {
            $this->url = 'index';
        }

        $this->url = explode('/', $this->url);

        $this->controller = implode('_', array_map('ucfirst', explode('_', str_replace('-', '_', array_shift($this->url)))));
        $this->method = explode('_', str_replace('-', '_', array_shift($this->url)));

        for($i = 1; $i < count($this->method); $i++)
        {
            ucfirst($this->method[$i]);
        }

        $this->method = implode('_', $this->method);
        $this->params = &$this->url;
    }

    /**
     * Initializes correct controller based on URL requested.
     */
    public function commit()
    {
        $class = "Elysium\\Controllers\\$this->controller";

        if(class_exists($class))
        {
            if(method_exists($class, $this->method))
            {
                if(empty($this->params))
                {

                    $ctrl = new $class;
                    $ctrl->{$this->method}();
                }
                else
                {
                    $ctrl = new $class;
                    $ctrl->{$this->method}($this->params);
                }
            }
            else if(empty($this->method))
            {
                $ctrl = new $class;
                $ctrl->index();
            }
            else
            {
                self::error(404);
            }
        }
        else
        {
            self::error(404);
        }
    }

    /**
     * Initializes default Error controller based on error code provided and shows the appropriate error page.
     * @param $code
     */
    public static function error($code)
    {
        switch($code)
        {
            case 404:
            {
                $ctrl = new Controllers\Error();
                $ctrl->notFound();
                break;
            }
            default:
            {
                break;
            }
        }
    }
}
  • 写回答

1条回答 默认 最新

  • dongna1593 2014-01-12 20:45
    关注

    Fixed it myself, the folder name must have uppercase letter just like the namespaces if you're adhering to PSR-0. If you don't want to change you can use composer's classmap.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?