简体   繁体   中英

how and why index.php is executing on every request in ZF2?

In Zend framework 2 ( Or generally any framework), when we requesting some url with module_name/action_name
ie /album/list

how and why index.php is executing on every request?

That is called the Front-Controller-Pattern .

What is a Front-Controller?

It's the single entry point of your application. The front controller (index.php) takes the request data and passes it to the appropriate services simply spoken.


Why should a Front-Controller be used?

In a web application you basically don't want to add overhead to every of your scripts. Instead you have one centralized point that delegates tasks.

For example a complex web application has a routing layer normally, a database layer, maybe caching, sessions and input handling are important.

When you had to provide these components in every standalone script (login.php, user.php, contact.php), you had to make all of the services available first. If you then needed to swap components it would be a pain to edit all of your files to apply your changes.


Example Workflow

That's where a front controller comes in handy. It takes the request and says: "Hey! I want to bootstrap my application. So load my bootstrapping class and pass in the necessary data of the request" .

The bootstrapping class says: "Mayday. I received orders from the front-controller. Need some routing here" .

Then my routing service provider is loaded.

"Oh and please give me a bit of sessions" . Now I want my session service to be made available.

And once all the background work is done, the front controller or one of it's delegated services can load the resource you actually requested.


Wrapping it up

In a middle- or large scale application you will not get around using a front controller. If you need to tie together a tiny application which doesn't depend on many service layers you can just drop the front-controller.

Otherwise you should absolutely use one. Once your application grows it automatically becomes harder to maintain. You will need a central point where your request is delegated to the services that are appropriate for handling that certain task.

Because of .htaccess file all requests passes to index.php

Reason:: As others says it is single entry pattern of framework.

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