Learning Laravel - part III

After getting the stylish welcome screen of Laravel 5 (find it here http://particle-in-a-box.com/blog-post/learning-laravel-part-ii), I was confident that Laracasts (https://laracasts.com) would get me up to speed with all the technologies I was not aware of while reading the docs. The videos are to the point, easy to work along with, and they don't make assumptions about the knowledge and expertise of the viewer. At the time of this writing I have worked along, experimented and side-tracked with the six first videos of the mini-series 'Laravel From Scratch' and in this post will talk about those experiences.

Basics

The first steps are basic, but taught me a ton about the file structure and inner workings of Laravel. HTTP get requests are passed to the PHP file routes.php that lives in the /app/http folder. In this file, using Laravel PHP syntax, you can put all your http rules and point an address to a specific page like so

Route::get('/', function() {
   return view('pagename');
});

This little piece of code would point the root domain name to a view called 'pagename'. Not to be mistaken for the ever-present 'views' module in Drupal, views in Laravel are basically the resources that will produce the HTTP of your website and you can put them in the /resources/views folder.

Blade or Twig: sorting out my tools


As it turns out, these views are no ordinary PHP as they suspiciously sport the fancy file extension *.blade.php. Blade is a templating engine, and before I proceeded with the lesson on this topic I wanted to find out more about it. As you may or may not be aware of, one of the big changes for Drupal themers was the implementation of Twig as a PHP templating engine in Drupal 8. I had already downloaded the docs on the technology, but left it at that up until now. As far as I could tell, Blade and Twig seemed to handle very similar tasks so I looked up the pros and cons of both engines. This blog post (https://medium.com/@barryvdh/comparing-blade-and-twig-templates-in-larav...) by Barry vd. Heuvel points out the differences between them and offers a way to use Twig in Laravel. I decided to go along with this method because Twig is more secure, it seems like a more mature project on its own, and it will definitely pay of in my future Drupal 8 projects. Installing TwigBridge (https://github.com/rcrowe/TwigBridge) was supposed to be easy but took me a long time anyway for two (possible) reasons. First reason was my being unaware there was a Laravel equivalent for drush cc. The Artisan command

php artisan cache:clear

This might turn out to be essential. After clearing the cache, however, still my twig templates did not return any HTML in the browser and I turned upside down every stone until I found it was a file permissions issue. So make sure your server has access to all the Laravel files and 'chown' everything appropriately after installation.

After installing TwigBridge and making it work I studied some Twig from the documentation (http://twig.sensiolabs.org/documentation), enough so I could follow the Laracasts videos about Blade and replace all the template syntax accordingly, using Twig. To my great pleasure everything worked as a charm. My Twig templates were being interpreted right, variables and arrays were passed from the page controller to the view without trouble, Twig template inheritance worked. This is fun.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.