Namespaces with Phalcon -


all controllers under namespace myapp\controllers so, documentation recommended, i've set default namespace it:

$dispatcher->setdefaultnamespace('myapp\controllers'); 

but need not organize controllers in folders namespace them , have friendly urls like: /features/featurex/ , /wizards/featurex/. example got myapp\controllers\features\featuresx , myapp\controllers\wizards\featuresx.

i believe shouldn't considered modules right? they're custom routes, routing documentation can't tell how to:

  • declare route defines namespace (e.g $router->add("/:namespace", ["namespace" => 1]);)
  • make above routing strategy used controllers. example, logincontroller, should remain in myapp\controllers namespace.

maybe can achieve using 1 router or dispatcher each one. experienced phalcon developer please give me light here?!

well, after time using phalcon can not flexible when decide use different approach found in project documentation. use of namespaces 1 of these cases.

multiple namespace levels aren't handled framework, still designed , extensible. can override pretty and, customization, can achieve behavior want.

the following i've done stuff organized in folders/namespaces , still make use of whole framework.

some of these information may useful in context, here report of legendary journey far...

phalcon vs namespaces

ok, if decide keep php source code organized within folders , namespaces, ready tweak major functionalities , much more explicit in implementations (i.e use full class paths everywhere).

basically you're going throw away of coolest automation framework has offer because they're convention based , these conventions seems defined without namespaces in mind.

but, despite work ahead, i've decided keep design decision; after all, we'd chose mvc framework reason, right?! want keep things organized. specially in huge projects important have source diluted in several namespaces/files improve maintainability.


directory structure

first off, should aware of directory structure i've picked understand i'm talking down below. believe me, i've spent hours days tweaking structure , reading structures recommended other mvc frameworks , after personal recommendation simple: pick less problematic one! means more decoupled/fragmented one.

this structure i'm using served me 2 years , fits whatever kind of code need throw @ it:

directory structure of project root

a clear understanding of directory/namespace structure removes considerable amount guesswork while implementing new stuff , helps write decoupled code unwittingly. if "waste" time caring in beginning means less painful refactors in future.


class loader

did noticed source/ folder , it's contents organized more or less psr standards demands?! reason phalcon's class loader psr-0 compliant;

so need register source/ folder loader:

$phloader = new phloader(); // considering public folder current __dir__ $phloader->registerdirs(['../source/']); $phloader->register(); 

...and voila! refer class (using full path when needed) , both php , phalcon internal implementations find it. example, register 1 of components in di container:

// @ first time service 'foo' needed // phalcon read file @ 'source/myapp/components/foo.php' // , call foo's constructor $di->set('foo', 'myapp\components\foo');  

router , controllers

if controllers on same namespace/folder can this:

$router->setdefaultnamespace('myapp\controllers'); 

nice!

but if have controller under myapp\controllers\foo\barcontroller?!

just default loader behavior, there's no effort router try interpret foo portion namespace level.

attow there's no workaround make router find controllers in "deeper" namespaces. solution far turn off default routing behavior , add manually four common patterns each namespace branch.

by "four common patterns" mean these routes:

:namespace/index/index when namespace defined

:namespace/:controller/index/ when namespace , controller name defined

:namespace/:controller/:action/ when namespace, controller , action name defined

:namespace/:controller/:action/:params when defined , includes parameters

to enable these routes namespaces i've replaced :namespace regex placeholder regex matches possible namespaces , implemented "converter" translates route correct namespace (i.e "/foo/bar/baz" --> ['namespace' => 'myapp\controllers\foo', 'controller' => 'bar', 'action' => 'baz']).

just illustrate i'm talking here's custom router i've write time ago , uses technique: https://gist.github.com/snolflake/9797835


models

you must follow the docs say. means, time refer model on phql (or relationship definitions) need use full class path.

views

by views mean template files. of course aren't under namespaces, since controllers are, automatic view picking doesn't work properly. views should picked manually:

namespace myapp\controllers\foo;  class barcontroller {     public function bazaction()     {         // path relative views dir you've set before         $this->view->pick('configurations/subscriptions/index.volt')         ... 

gladly can make use of event beforeexecuteroute automatically pick view based on own conventions.

conclusion

it seems phalcon aiming more in simple/trivial project, that's reasonable , these boilerplate code application more fancy rest of framework make worth job later on.

meanwhile hope next versions include more namespace-friendly features.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -