【问题标题】:PHP class - error in linux but works in windowsPHP类 - linux中的错误但在windows中有效
【发布时间】:2018-02-01 07:51:03
【问题描述】:

我创建了一个类并定义了它的命名空间,并在索引中使用use 后跟它的命名空间来调用它,此外,我还创建了自动加载器文件。它在 windows 上运行一切顺利,但如果在 linux 中我收到错误消息

致命错误:未捕获的错误:在/opt/lampp/htdocs/pos-hm-git/index.php:6 中找不到类'lib\MVC\Router' 跟踪线索:#0 {main} 被扔进/opt /lampp/htdocs/pos-hm-git/index.php 在第 6 行

Structure Folder

index.php

<?php
require_once __DIR__ . '/autoloader.php';

use lib\MVC\Router;

$kernel = new  Router($_GET);
$controller = $kernel->getController();
$controller->executeAction();
 ?>

autoloader.php

<?php

spl_autoload_extensions(".php");
spl_autoload_register();
?>

Router.php

<?php
namespace lib\MVC;

class Router
{
  private $controller;
  private $action;
  private $urlparams;
  private $controller_namespace = "\\Controllers\\";
  private $base_controller_name = "lib\\MVC\\BaseController";

  function __construct($url)
  {
    //memasukan semua parameter dari index yang di tangkap dengan GET
    $this->urlparams = $url;
    //menentukan nama controller
    if (empty($this->urlparams['controller'])) {
        $this->controller = $this->controller_namespace . "Home";
    }else {
        $this->controller = $this->controller_namespace . $this->urlparams['controller'];
    }

    //menentukan aksi yang akan dijalankan
    if (empty($this->urlparams['action'])) {
        $this->action = "index";
    }else {
        $this->action = $this->urlparams['action'];
    }
  }

  public function getController()
  {
    if (class_exists($this->controller)) {
        $parent = class_parents($this->controller);

        if (in_array($this->base_controller_name, $parent)) {
          if (method_exists($this->controller, $this->action)) {

            return new $this->controller($this->action, $this->urlparams);

          }else {
            throw new \Exception("Aksi tidak ditemukan, braaaaay", 1);
          }
        }else {
          throw new \Exception("class untuk controller salah braaay, coba sekali lagi", 1);
        }
    }else {
      throw new \Exception("Controller tidak ditemukan, braaay", 1);
    }
  }

}


?>

【问题讨论】:

  • 看起来您默认使用 spl_autoload。我认为使用您的包含路径。如果您编辑:您的问题包括代码(而不是链接图像),请添加get_include_path() 的输出;
  • php.net/manual/en/function.spl-autoload.php,在 cmets (#15 daniel) 中,它建议这个函数默认使用小写的文件名映射的类名。这会在 *nix 上中断。也许试试 PSR-4 兼容的自动加载器,或者编写自己的函数。
  • 好的,我试试

标签: php class web


【解决方案1】:

我赌的是文件名。对于 Linux,它是两个不同的文件 - MVC.php 和 mvc.php。对于 Windows,它是相同的 - 所以它可以工作。

【讨论】:

  • 非常感谢,兄弟。但我很确定刻字很好。如果您不介意,请查看图片。
猜你喜欢
  • 2014-10-23
  • 2020-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-26
  • 1970-01-01
  • 2020-01-28
相关资源
最近更新 更多