简体   繁体   中英

PHP Namespace collision with Phalcon, Vagrant, Apache2

I have this strange problem that I can best describe as "namespace leakage". I have setup a vagrant box running Apache2 with VHosts setup to replicate the production server in terms of domains & sub domains. I have edited my local machine's hosts file accordingly, and my VHosts work fine.

I have also created a skeleton/base app coded with PhalconPHP. It's a multi modular app with all the generic features I require to develop my apps (actually redevelop a load of very old, outdated apps). The skeleton app works fine.

The problem I have is that if I go to app1.dev in my browser, it works. Then if I go to app2.dev, app2 is clearly trying to load some components - views etc from app1 and is giving errors. However, close the browser and try again by going to app2.dev and it now works fine. Then go to app1.dev and that is now broken and trying to load components from app2! I managed to track this odd behaviour down to namespace collision.

It has to be namespace collision because my apps are all based on the skeleton app and use it's name spaces, which are obviously the same for generic parts of the app - modules such as App\\backend , App\\frontend etc. If on a broken app, I navigate in my browser to a part of that app that is unique, and therefore has a unique namespace, it works fine because there is no collision! Also a couple of apps are coded with Codeigniter3 which does not use namespaces, and those apps do not have this issue. It only effects the Phalcon apps with namespaces.

I will add that each app uses .htaccess to direct requests to the front controller located in public/ directory.

Options FollowSymLinks
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule  ^$ public/    [L]
    RewriteRule  (.*) public/$1 [L]
</IfModule>

I wondered if the .htaccess was the issue, but I think I've ruled that out. It is doing what it is supposed to do.

Here's an example of one of my VHost settups in apache - they all follow this pattern.

<VirtualHost *:80>
    ServerName app1.dev
    ServerAlias www.app1.dev
    DocumentRoot /vagrant/www/app1
    <Directory "/vagrant/www/app1">
            Options Indexes Followsymlinks
            AllowOverride All
            Order allow,deny
            Allow from All
            Require all granted
    </Directory>
</VirtualHost>

Changing all the namespaces throughout every app would be a pretty major job, and I don't think that should be necessary. I don't think this should be an issue on the production server as that uses CloudLinux/Centos & WHM, but it is a bit of a worry!

Clearly namespaces should not collide across different document routes and VHosts right? What am I missing?

I have solved similar problem in Apache using different local IP's for every app:

/etc/hosts

127.1.0.1   edu.l
127.1.1.1   tech.l
...

/etc/apache2/sites-available/001-blogs.conf

<VirtualHost edu.l:80>
    ServerName edu.l

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/blog/edu

    ErrorLog ${APACHE_LOG_DIR}/edu.error.log
    CustomLog ${APACHE_LOG_DIR}/edu.access.log combined

</VirtualHost>

<VirtualHost tech.l:80>
    ServerName tech.l

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/blog/tech

    ErrorLog ${APACHE_LOG_DIR}/tech.error.log
    CustomLog ${APACHE_LOG_DIR}/tech.access.log combined

</VirtualHost>
...

You can ommit configuring names in hosts ans use IP's in .conf tho.

It works because if you define few apps on same IP and port, Apache seems to remember directory it is working on and mismatching files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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