Zend framework default MVC implementation

2019-08-13 02:11发布

问题:

I have been using Zend Framework for a while now. I am having an issue since de beginning an after reading a lot of documentation i was wondering why all exemples dont mention it.

If I am right, the MVC design pattern favorise a single entry point to interract with the application. With Zend its the index.php file in the public folder.

I tried to use this way with the default (v 1.09) .htaccess that Zend provides :

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

and the default index.php file :

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

/** Zend_Application */
require_once 'Zend/Application.php';

// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);


$application->bootstrap()
            ->run();

But so far I have been unable to use the single entry point, I had to duplicate the index file and give it the name of the related controller. This way instead of getting a 500 server error, I was able to get the page to load normally.

So, my question is : What am I getting wrong to get my application to work? Is it the normal way of doing things?

Thanks

EDIT @ 13h55

I have reconfigured my server to access the zend installation through a virtual host instead of an alias and it seems that that was the error.

I went from an alias looking like :

Alias /elul/ "c:\wamp\www\elul\trunk\elul\public/" 

<Directory "c:\wamp\www\elul\trunk\elul\public/">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride all
        Order allow,deny
    Allow from all
</Directory>

To a virtual host looking like :

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot "C:\wamp\www\examen_fle\trunk\examen_fle\public"
    ServerName examen.elul.local
    ErrorLog "logs/your_own-error.log"
    CustomLog "logs/your_own-access.log" common
    <directory "C:\wamp\www\examen_fle\trunk\examen_fle\public">
        Options Indexes FollowSymLinks
        AllowOverride all
        Order Deny,Allow
        Deny from all
        Allow from 127.0.0.1
    </directory>
</VirtualHost>

Since it runs locally, i also had to edit the file hosts in C:\WINDOWS\system32\drivers\etc to add examen.elul.local in the file

127.0.0.1       examen.elul.local

回答1:

If you prefer using an alias instead of a virtual host, you might want to use the RewriteBase or you may prepend the base url to the frontcontroller:

RewriteBase /elul/
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

or

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /elul/index.php [NC,L]

should work too... I use this one though:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(js|ico|gif|jpg|png|css)$ /project_name/index.php

It's a simpler rule but I'm not sure if it really affects the performance or covers all the possibilities.