简体   繁体   English

Amazon EB 在自动加载我的 PSR-4 命名空间类时遇到问题

[英]Amazon EB Having Issues Autoloading My PSR-4 Namespaced Classes

I wanted to add PSR-4 namespacing for my classes:我想为我的类添加PSR-4 命名空间

- app
-- autoloader.php
-- bootstrap.php -> `include 'autoloader.php'`
- public
-- index.php -> `include './../app/bootstrap.php'`
- controllers
-- Test.php

controllers/Test.php:控制器/Test.php:

<?php

namespace Controllers;

class Test{}

app/autoloader.php (simplified): app/autoloader.php(简化版):

<?php
spl_autoload_register('myAutoloader');

function myAutoloader($className) {
    include "./../$classname.php"
}

Then at some point I call Controllers\\Test::someMethod() .然后在某个时候我调用Controllers\\Test::someMethod()

Locally , this loads fine.在本地,这很好加载。 But when I deploy it to my AWS EB instance , it fails.但是当我将它部署到我的AWS EB 实例时,它失败了。 So I ran tests to narrow it down to this:所以我进行了测试以缩小范围:

var_dump(file_exists('./../controllers/Test.php')); // local: bool(true), AWS: bool(true)
var_dump(file_exists('./../Controllers/Test.php')); // local: bool(true), AWS: bool(false)***

On the AWS EB instance, the filename with the capitalized Controllers cannot be found, but the lowercased controllers can.在 AWS EB 实例上,找不到大写的Controllers的文件名,但可以找到小写的controllers

How can I configure my EB instance to allow for mixed capitalization?如何配置我的 EB 实例以允许混合大小写?

Did some further digging and found that the issue is the Elastic Beanstalk instance is running on Linux.进一步挖掘,发现问题是 Elastic Beanstalk 实例在 Linux 上运行。 My local environment is a Mac.我的本地环境是 Mac。

file_exists() and include|require() are case-insensitive on Mac and Windows, but it is case-sensitive on a Linux. file_exists()include|require()在 Mac 和 Windows 上不区分大小写,但在 Linux 上区分大小写

So in order to autoload classes without the aid of a library or framework abstraction, I needed to capitalize my directories.因此,为了在不借助库或框架抽象的情况下自动加载类,我需要将目录大写。 Unfortunately, I wouldn't be able to do any of the fancier PSR-4 standards:不幸的是,我无法执行任何更高级的 PSR-4 标准:

\Acme\Log\Writer\File_Writer => ./acme-log-writer/lib/File_Writer.php

...but I'm sure there are libraries out there that can convert a $className to a PSR4 filepath. ...但我确信有一些库可以将$className转换为 PSR4 文件路径。 In the meantime, I'll stick to capitalizing my namespace-directories.与此同时,我将坚持将我的命名空间目录大写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM