2009年2月24日星期二

Re: [fw-mvc] modulear application with router for each controller in each application

-- vadim gavrilov <vadimg88@gmail.com> wrote
(on Tuesday, 24 February 2009, 08:17 AM +0200):
> Thanks,
>
> I assume the '_' is what sets it to know it's a sub directory. Is there a way
> to replace the '_' with a '/' so the link will be admin/tools/other/
> [actionname] i know i can do this with the dispatcher but not sure how.

Actually, no. The reason _ was chosen over / is because / is used to
separate discrete segments of the URL; it's difficult to reliably
determine that a segment of the url indicates a directory without doing
stat calls on each pass.

As an example, let's look at this structure:

application/
controllers/
Admin/
FooController.php
Public/
FooController.php
News/
FooController.php

Now, let's consider you want to match /admin/foo/bar. How would the
router try to match this?

The default route is, basically, ':controller/:action/*' -- so, knowing
this, it would try to execute AdminController::fooAction() with an empty
"bar" parameter.

Now, the default route also allows for an optional "module" segment. In
that case, if any modules have been added to the front controller, it
will try to match the first segment against the list of modules; if any
match, it will then grab that controller in that module.

You could potentially leverage this information, and do the following:

$front->addControllerDirectory(APPLICATION_PATH . '/controllers/Admin', 'admin')
->addControllerDirectory(APPLICATION_PATH . '/controllers/Public', 'public')
->addControllerDirectory(APPLICATION_PATH . '/controllers/News', 'news');

The problem with this approach is that modules are supposed to be
self-contained "applications" -- a module's directory structure should
mimic the recommended directory structure:

application/
modules/
admin/
controllers/
FooController.php
views/
scripts/
public/
controllers/
FooController.php
views/
scripts/
news/
controllers/
FooController.php
views/
scripts/

If you don't follow that structure, you then need to do a bunch of other
customizations; the ViewRenderer's mappings will need to be altered so
that they match your own directory structure, for instance.

Another solution is to write your own route that makes the assumption
that the first segment of the path always indicates a directory under
controllers/. This would not be hard to do -- take a look at the module
route (Zend_Controller_Router_Route_Module) to get some ideas.

> The thing i want to do with the router is have a file called router.php inside
> every controllers sub directory that will add routes to certain places. I
> didn't quite grasp the way i need to use the router class and from reading the
> documentation (what i read so far) it doesn't clearly say where you need to
> define the new instance of the router class. If it's in the bootstartup.php
> file then how can i access it later from each and every controller? I can't
> create a new instance in every controller beacuse that will be a bad design
> practice. How about creating a master controller that will extend the zend
> abstract and that one will create the instance of the router? Then i could just
> extend the master controller in every controller i have in my controllers
> directory? Is this something usable?

Adding routes to Zend_Controller_Action (which is what I assume you mean
by "the zend abstract") is too late -- routing occurs in the front
controller, before any action controllers are instantiated.

What you need to do is inject routes into the router prior to to
routing. This can either be done during bootstrapping, or in a front
controller plugin with a defined routeStartup() hook.

Honestly, though, it sounds to me like what you're looking for is this:

http://framework.zend.com/manual/en/zend.controller.modular.html

Based on what you described -- custom routes per subdirectory of
controllers -- a modular directory structure is the recommended way to
achieve it.

This is going to get a ton more powerful and meaningful in 1.8, due to
the introduction of Zend_Application, and based on what you're trying to
do, I think you'll be able to leverage the idea of a "bootstrap per
module." In this paradigm, you can have a bootstrap class per
module, and within it add initialization routines that create new
routes to add to the router.


> On Tue, Feb 24, 2009 at 1:36 AM, Tomoaki Kosugi <kipspro@gmail.com> wrote:
>
> Hi
>
> This is my guess,you need to set up two as below.
>
> 1. Controller Setup
> 2. Router Setup
>
> 1. Using action controllers in sub directories
> /admin/tools/otherController.php
> is mapped to below↓
> className:
> Admin_Tools_OtherController
> you can access this controller in this uri.
> /admin/tools_other/[actionname]/*
>
> 2.Router Setup
> you can add routes from the uri "/admin/tools/other"
> to the controller named "tools_other" .
>
> Excuse me ,I am not good at English.
>
> regards
>
> --
> noopable:
> Tomoaki Kosugi
>
>
> 2009/2/24 vince. <vadimg88@gmail.com>:
> >
> > How can i set a rewrite rule class/instance across the entire application
> > that i could use sub directories inside the main controllers directory
> for
> > each module? Meaning so i could have this structure:
> >
> > /application
> > /modules
> > /admin
> > /controllers
> > /index
> > /indexcontroller.php
> > /othercontroller.php
> > /tools
> > /indexcontroller.php
> > /othercontroller.php
> > /settings
> > /indexcontroller.php
> > /othercontroller.php
> > /layouts
> > /site
> > /controllers
> > /index
> > /indexcontroller.php
> > /othercontroller.php
> > /tools
> > /indexcontroller.php
> > /othercontroller.php
> > /settings
> > /indexcontroller.php
> > /othercontroller.php
> > /layouts
> > /library
> >
> >
> > and i could enter the file admin/controller/tools/other as follows:
> > admin/tools/other trough the url. and basically use sub directories
> inside
> > the controllers directory. what do i need to setup?
> >
> > Thanks.
> >
>
>
>
>
>
> --
> Vincent Gabriel.
> Lead Developer, Senior Support.
> Zend Certified Engineer.
>
>
>
>

--
Matthew Weier O'Phinney
Software Architect | matthew@zend.com
Zend Framework | http://framework.zend.com/

没有评论: