Structuring larger Dancer Applications
Perl Dancer is a simple web application framework. It is so simple that you can start with just a single file. But there is hardly any documentation on how to structure your application once it grows beyond that.
The problem is when your application is growing. You usually add forms, models and model specific controllers. Perl dancer does not offer a best practice for structuring larger applications, but since this is perl it is fairly simple to structure your application. What I am presenting here is what has worked very well for me developing some larger dancer based applications. Your milleage may vary.
As an example I will show you how you can structure your controllers, but the same goes for models, forms, and helpers. So let’s start a new dancer application by issuing the following command in a terminal:
This will create a folder called SampleApp, with the default dancer file structure:
Now if you change into the SampleApp directory and start the application with the following command:
and open a browser with the following url:
you sould see the default dancer start page. So far so good :)
So let’s stop the application by pressing CTRL+C.
The first thing we are going to do is create separate controllers. So change into the lib folder and create a new directory:
The lib folder should now contain the following:
Next we are going to create an index controller:
Now it is time to start your favorite editor and open the file SampleApp/lib/SampleApp.pm which should look like this:
Change the file so it looks like this:
Next open the file SampleApp/Controllers/Index.pm and paste the following code:
Save the file and switch back into the terminal. Start the application again and open the url:
You should still see the default dancer start page.
Ok let’s create a new controller:
Edit the new controller and paste the following code:
Next we need to load the new controller. Add the following line to the SampleApp.pm right below the Index.pm controller:
This adds the new controller under the prefix ‘/contact’. Now start the application again and open the url:
You should now see the text you have entered in the return statement (Show Contact Form). Notice that even though in the controller we have:
the page is under the ‘/contact’ url. This is because of the prefix statement in the load_app function, which mounts the controller under the specified prefix.
Your projects file structure should look something like this:
Of course there are some things I have not done here, like add all new created files and folders to the MANIFEST file. Also there are some shortcommings with this method, but most of the time this works, at least for me.
You can also add more folders in the SampleApp directory, like Models or Forms and add your model and form files in these. Hope this is of help to some of you.
Also if you are interested you can find a larger sample, including models and helpers in my DancerPrelaunch application on GitHub: https://github.com/pwfraley/DancerPrelaunch