开发者

Zend Framework-does every webpage have it's own controller and indexAction and view?

开发者 https://www.devze.com 2023-01-02 14:51 出处:网络
I\'m wanting to be sure that I am setting things up correctly.On a typical website with 10 pages, would each page have it\'s own controller with it\'s own IndexAction and it\'s own View folder with it

I'm wanting to be sure that I am setting things up correctly. On a typical website with 10 pages, would each page have it's own controller with it's own IndexAction and it's own View folder with it's own index.phtml as a view?

Or do you have one controller wi开发者_StackOverflow中文版th multiple Page1Action, Page2Action, etc and have multiple differently named view.phtml pages within view/index folder?

I'm leaning toward the former because then I can have a cleaner controller for each page...

Is there a standard, or is it subjective?


Your sitemap would play a major role in this question. But, in lieu of that, here's a few examples.

Example 1. Flat

/foo
/bar
/baz

You'll probably want to use separate controllers: Foo/IndexController.php, Bar/IndexController.php, and Baz/IndexController.php with each having an indexAction() method to pass information to your view (once again separate).

Example 2. A Little Bit Lower Now

/foo/bar
/baz

You'll only need two controllers: Foo/BarController and Baz/IndexController. If /foo needs a landing page you'll have to throw in a Foo/IndexController.php to be safe. Your actions are still indexAction(). Because you've not gone deep enough to hit that third level, your views are still index.phtml.

Example 3. Straight Line

/foo/bar/baz

You're down to onc controller: Foo/BarController.php. If you need landing pages for /foo and /foo/bar you'll need another controller for /foo (Foo/IndexController) and an indexAction() for both. With /foo/bar/baz you're actually down to a slightly different action now too - bazAction() (inside Foo/BarController.php). Your view is now baz.phtml.

Summary.

The wider the sitemap the more controllers you have and fewer actions. The more narrow the sitemap the fewer the controllers and more actions.

Postscript.

I should also state, this is also contingent on using default routing patterns. If you do something a little more sophisticated in routing patterns, this is all shot out the window. Sometimes we use routes to keep the number of classes manageable. When we have a wide sitemap it's possible to create some custom routes and use __call() within a controller to hand-off view data appropriately. Just another way to skin this cat.


Typically you would create one Controller for a related group of Actions. What related means is subjective.

Very roughly speaking, a group of related Actions operates on the same Model. At least that's a good starting point, but it rarely works out that simply, because few real-world applications consist solely of CRUD operations on each Model.

If you decouple the Model from being simply a data access component, you can more sensibly define a logical grouping of Controller Actions for a Model. A Model is where the majority of your code exists for business logic. Database persistence is just an (optional) internal detail of the Model, to preserve state from request to request. But a Model doesn't necessarily use a database. It could be standalone, or it could be an aggregation of other Model objects.

By default, each Action has its own View script. But this is also just a starting point, because you could use Layouts to make many View scripts share some if their markup in common, and you could use View Helpers and Partials and so on.

0

精彩评论

暂无评论...
验证码 换一张
取 消