【问题标题】:PHP Composer autoloadPHP Composer 自动加载
【发布时间】:2021-03-18 06:51:48
【问题描述】:

我使用无脂肪框架编写代码并尝试使用这种结构

app/
vendor/
public/
   index.php

composer.json

{
   "require": {
      "bcosca/fatfree": "^3.7"
   },
   "autoload": {
      "psr-4": { "App\\": "app/" }
   }
}

使用本地服务器 php -S 127.0.0.1:8000 -t public/ 我的网站显示正确,但部署在远程服务器上,带有 subdomain.domain.com 我有经典错误 p>

HTTP 404 (GET /test)

[hb/vendor/bcosca/fatfree/lib/base.php:1716] Base->error()
[hb/public/index.php:34] Base->run()

如果我尝试在我的命名空间 App 中调用一个类,我会遇到错误
内部服务器错误

Class 'App\Test' not found [/home/clients/zzz/web/test/public/index.php:26]

[/home/clients/zzz/web/test/vendor/bcosca/fatfree/lib/base.php:1955] {closure}()
[/home/clients/zzz/web/test/vendor/bcosca/fatfree/lib/base.php:1754] Base->call()
[index.php:35] Base->run()

我在带有 index.php 的 public 文件夹中的 .htaccess 是:

RewriteEngine On
RewriteBase /
RewriteRule ^(app|tmp)\/|\.ini$ - [R=404]

RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /index.php [L,QSA]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]

谢谢大家

【问题讨论】:

  • 请分享更多细节。您还没有为App\Test 提供任何代码,因此在我看来很明显会显示此错误消息
  • 谢谢你用“composer dump-autoload -o” 我想问题是文件夹名称中的大写或小写:app/app/Test.php app/Controller/Test2.php
  • 请通过编辑为您的问题添加所有说明
  • 如果您使用 httpd 作为您的网络服务器,AllowOverride All 是否打开?否则它甚至不会查看您的 htaccess 文件。
  • 在您的 layout.htm 或容器 html 页面中,您应该有一个标签用于声明您的基本 url <base href="https://www.example.com/">。这应该有助于为您的链接、图像、样式表等设置基本 url。

标签: .htaccess composer-php fat-free-framework psr-4


【解决方案1】:

在您的 public/index.php 中,在执行任何其他操作之前将当前工作目录更改回项目根路径:chdir('../');

应该可以解决它。

【讨论】:

  • 请在您的答案中添加一些解释,以便其他人可以从中学习 - 为什么这会有所帮助?现有代码在一个系统上运行,但在另一个系统上不行,是否有任何理由?
猜你喜欢
  • 2018-07-11
  • 1970-01-01
  • 1970-01-01
  • 2017-11-26
  • 2013-02-17
  • 1970-01-01
  • 2017-09-02
  • 2018-11-26
  • 2017-02-01
相关资源
最近更新 更多