separating code into packages
I have not reached that point yet but as your application grows it might be better to move some code and with that some route definition to separate files. For example if I have a Dancer application called Foo I might want to move all the user related methods get '/user/this' => sub { ... }; get '/user/that' => sub { ... }; get '/user/other' => sub { ... }; to a package called Foo::User; I can then include use Foo::User; in the main Foo package and the above routes are added. Within Foo::User the /user prefix seems to be too repetitive so it might be better to have just get '/this' => sub { ... }; get '/that' => sub { ... }; get '/other' => sub { ... }; and mount that package to '/user' within the Foo application. (which by itself might have been mounted to some path. If I understood it correctly from http://advent.perldancer.org/2010/2 and from the recent presentation of Sawyer, this would be getting similar to what MVC frameworks do and what you referred to as "Namespace matching". As I was playing with this idea I implemented a small example I call Dancer::Plugin::MVC for now that would do this automatically. You just include use Dancer::Plugin::MVC; in the main Foo package and during its import() call it will load all the modules in the subdirectories of Foo. So far it simply loads the modules it finds which is quite simple. I'll try to check if I can mount the various modules in other places as well though in that case I might not want to do it for all the modules and the mount point should be the same as the package name. What do you think? Is this totally contradicting the philosophy of Dancer? Gabor
Gabor Szabo wrote:
I have not reached that point yet but as your application grows it might be better to move some code and with that some route definition to separate files. For example if I have a Dancer application called Foo I might want to move all the user related methods
get '/user/this' => sub { ... }; get '/user/that' => sub { ... }; get '/user/other' => sub { ... };
to a package called Foo::User;
I can then include
use Foo::User;
in the main Foo package and the above routes are added.
Within Foo::User the /user prefix seems to be too repetitive so it might be better to have just
get '/this' => sub { ... }; get '/that' => sub { ... }; get '/other' => sub { ... };
and mount that package to '/user' within the Foo application. (which by itself might have been mounted to some path.
Have you looked at `load_app`? package Foo.pm use Dancer ":syntax"; load_app "Foo::User", prefix => "/user";
If I understood it correctly from http://advent.perldancer.org/2010/2 and from the recent presentation of Sawyer, this would be getting similar to what MVC frameworks do and what you referred to as "Namespace matching".
As I was playing with this idea I implemented a small example I call Dancer::Plugin::MVC for now that would do this automatically. You just include
use Dancer::Plugin::MVC;
in the main Foo package and during its import() call it will load all the modules in the subdirectories of Foo. So far it simply loads the modules it finds which is quite simple.
I'll try to check if I can mount the various modules in other places as well though in that case I might not want to do it for all the modules and the mount point should be the same as the package name.
What do you think? Is this totally contradicting the philosophy of Dancer?
Gabor _______________________________________________ Dancer-users mailing list Dancer-users@perldancer.org http://www.backup-manager.org/cgi-bin/listinfo/dancer-users
-- Puneet Kishor
On Fri, Dec 24, 2010 at 5:32 PM, Puneet Kishor <punk.kish@gmail.com> wrote:
Gabor Szabo wrote:
I have not reached that point yet but as your application grows it might be better to move some code and with that some route definition to separate files. For example if I have a Dancer application called Foo I might want to move all the user related methods
get '/user/this' => sub { ... }; get '/user/that' => sub { ... }; get '/user/other' => sub { ... };
to a package called Foo::User;
I can then include
use Foo::User;
in the main Foo package and the above routes are added.
Within Foo::User the /user prefix seems to be too repetitive so it might be better to have just
get '/this' => sub { ... }; get '/that' => sub { ... }; get '/other' => sub { ... };
and mount that package to '/user' within the Foo application. (which by itself might have been mounted to some path.
Have you looked at `load_app`?
package Foo.pm use Dancer ":syntax";
load_app "Foo::User", prefix => "/user";
I tried that but that overrides any previous setting of 'prefix'. So if I mounted Foo to lets say '/foo' I'd like /user to become /foo/user The example you gave works but it mounts /user to the root directory. Presumably what I should do is fetch the current prefix and append the additional prefixes to that so I'd need to do is something like this: my $prefix = prefix(); load_app "Foo::User", prefix => "$prefix/user"; unfortunately prefix() in that case will reset the current prefix and always returns 1. Even though the docs says you need to call prefix undef; for that which is IMHO a bit misleading. Reading the set_prefix method in Dancer::App it is unclear to me what is the gain in always returning 1. Maybe the prefix() function could be changes to always return the previous value or there could be a separate method get_prefix, that would return the current prefix. In the end I found some other way. I am not sure if it is blessed by the Dancer developers or not: my $prefix = Dancer::App->current->prefix(); load_app "Foo::User", prefix => "$prefix/user"; Dancer::App->current->prefix($prefix); Gabor
On Sat, Dec 25, 2010 at 11:01 AM, Gabor Szabo <szabgab@gmail.com> wrote:
In the end I found some other way. I am not sure if it is blessed by the Dancer developers or not:
my $prefix = Dancer::App->current->prefix(); load_app "Foo::User", prefix => "$prefix/user"; Dancer::App->current->prefix($prefix);
Actually, I do something much simpler. I have a Foo::User.pm, which has: package Foo::User; use Dancer ':syntax'; prefix '/user'; get '/' => sub {...}; get '/view/' => sub {...}; -- Then from Foo.pm, I set up: load path( qw/ Foo User.pm / ); That's it.
On Sat, Dec 25, 2010 at 1:35 PM, sawyer x <xsawyerx@gmail.com> wrote:
On Sat, Dec 25, 2010 at 11:01 AM, Gabor Szabo <szabgab@gmail.com> wrote:
In the end I found some other way. I am not sure if it is blessed by the Dancer developers or not:
my $prefix = Dancer::App->current->prefix(); load_app "Foo::User", prefix => "$prefix/user"; Dancer::App->current->prefix($prefix);
Actually, I do something much simpler.
I have a Foo::User.pm, which has: package Foo::User; use Dancer ':syntax'; prefix '/user';
get '/' => sub {...}; get '/view/' => sub {...}; --
Then from Foo.pm, I set up: load path( qw/ Foo User.pm / );
That is indeed simpler but it will break if you mount the whole Foo application to /xyz as your user will still be at /user and not at /xyz/user Gabor
On Sat, Dec 25, 2010 at 2:58 PM, Gabor Szabo <szabgab@gmail.com> wrote:
That is indeed simpler but it will break if you mount the whole Foo application to /xyz as your user will still be at /user and not at /xyz/user
If I move it to /xyz, it will be placed in /xyz/user and it will still load it correctly.
participants (3)
-
Gabor Szabo -
Puneet Kishor -
sawyer x