Experimenting with Laravel
I’m sitting here working on Gastropoda on a train to Norrköping. A couple of days ago I started experimenting with Laravel after the friendly folks on /r/php provided some invaluable input to my vanilla PHP vs framework question.
Laravel has been easy to set up. All in all getting Vagrant up and running and Laravel installed took maybe up to an hour. I’ve been trying to think of a good way to port my existing setup to an MVC-based framework. I’m basically going to paste a copy of what I posted on the laravel.io forum to explain the work ahead.
Basically, in the vanilla PHP version I have pages, main object classes, and manager object classes. Two examples:
I should note that snail data is not limited to being displayed on snail.php - snails are also instantiated and displayed on jar.php, for example.
Currently, in the no-framework version, I’ve been structuring every main object class (ie snail.class.php) in a way that ensures the class only contains methods that pertain to a specific instance of an object. So you’d never see a method like
findSnailsByJarID() in snail.class.php because it would often return more than one snail. Instead, those sorts of things - methods that require pulling out multiple snails based on given requirements or trigger any sort of events on a set of snails/jars/whatever would go into snailmanager.class.php, jarmanager.class.php, etc.
An example of stuff in snail.class.php would be:
An example of stuff in snailmanager.class.php would be:
nightlyEnergyDepletion (which would call depleteEnergy for each relevant snail), etc.
How should I split this up in an MVC pattern as pertaining to Laravel? Where should the logic pertaining to each instance of an object go? Some people have suggested putting everything into the controller, others in the model. Most seem to suggest not separating methods that affect one instance of an object from methods that perform actions on multiple objects (so there would be no separation such as the one I had with snail.class.php and snailmanager.class.php).
Anyway, while I do more research I’ve kind of been hacking it together with the expectation of later changing it as I learn. So the way I have it set up now for a jar is:
The data I need to pass to the view is retrieved via a View Composer, which communicates with JarController, SnailController, and UserController to get information about a jar’s owner and all the snails within it. In a sense my controllers in Laravel kind of take on the role of the managers in the vanilla PHP version, except when data needs to change the view would never talk directly to the model but go through a relevant controller. The view also no longer decides anything (eg, whether to show the Edit form for a jar if the user viewing a jar is its owner). This is all done before, in the view composer, and from there the edit form is displayed if it is available.
Anyway, I am going to have to change how this works a likely stupid amount of time as I go.