yaf
yaf copied to clipboard
Forward action between two modules on this same controllers name
When we have this same controller in two modules, example:
/application
|/constrollers
| |--/Index.php
|/modules
| |/Admin
| | |/controllers
| | | |/Index.php
And in /modules/Admin/controllers/Index.php we do:
...
$this->forward('Index', 'index', 'index');
...
Then run indexAction in IndexController in Admin module but we want Index module.
My suggestion is to use namespace for controllers and models, with the name of the module. No namespace is the default module. Example
namespace Admin;
class IndexController
{
public function indexAction()
{
$model = new \Admin\UserModel();
}
}
+1 for the namespace, although i dont know if this has any conflict with routes.
hmm, PHP implemented namespace in a very tricky way, that is, if you define a class Foo in a Namespace Bar, the really class name is 'Bar\Foo",
in the opinion of Yaf loader, it equal to class Bar_Foo, so if we supports namespace in module, how can the Yaf Loader tell controller A in module B (B_A) from the ctronller B_A?
I think in modules controller's name must be like Bar\Foo or Bar_Foo (if namespaces is off). Routes definitions can look this same, because we can get namespace from module name. Only problem is not backward-compatible, but in this form modules don't work correctly.
So to answer your question, Yaf loader know it is namespaces on or off (from php.ini), know what module it is from routes or default route like /module/controller/action so Loader can load good controller. But this is only a suggestion there may be a better solution.
hmm, php.ini is a way(bc), let me think about it. thanks
There is an issue in the current implementation of modules. Controllers in a module can have the same name with controllers in another module. So Yaf loader can not load the right controller, in some cases (forwarding, extending). Defining a module as a namespace then may partial solve the problem.
Assuming the controllers IndexController
and Admin\indexController
. IndexController
can be found in default module (index) and Admin\IndexController
can be found in Admin module.
But maybe there is also an Admin\IndexController
in the default module (index). The right way should be
Index\IndexController
in Index module, Admin\IndexController
in Admin module and Index\Admin\indexController
in Index module. So even default module (index) should have a namespace.
This can work without namespaces by replacing \
with _
.
Right now, Yaf defines the current module from the Request right? That is why can handle controllers with the same name in different modules. But when there is a need to extend a controller between modules or forward a controller from a module to another, then appears the above problem.
Hey
Just encountered this issue. Is there any decision about this issue?
Thanks, Ofer
同感