Learning Laravel

This week I have decided to dive into learning a new PHP framework. I have been hearing a lot about Laravel recently. The vibe has been positive about this relatively new framework so I figured I would dive in and see what the buzz was about.
My goal is basically 2 fold. Firstly I want to rework an old website I bought a few years back which is getting ok traffic (a couple of hundred visits a day) but has really bad php code (all code is directly in the page). Secondly I wanted another technology to list on in my skill set. I have a ton of MVC and object oriented experience but in the Internet industry you should never stop pushing yourself to learn new things.

Going into this I already know this is going to be more than a one week process but I hope at least this week to learn the fundamentals of Laravel so that I can focus on reworking the site in following weeks.

You can find the Laravel framework here – http://laravel.com/

Day 1 – The install

The install of Laravel was easy. One if the things Laravel boasts Heavly about is how good the documentation is. In reality I didn’t actually need the documentation for the install, it pretty much worked out of the box. It was worth giving it a read through though otherwise I would have missed the configuration about the secret key which doesn’t need to be changes but should be.

Laravel Documentation – http://laravel.com/docs

Day 2 – Getting to grips with routing

After the install the first thing I wanted to do was to be able to actually set up a dummy URL and see content appear under it. In other frameworks this can involve having to set up views, controllers, models etc but in Laravel just to see a basic page you don’t have to do all that heavy lifting.

In Laravel you access pages and code through routes. You need to set up a route in order to be able to get to anything on the site. Once again here the documentation provided by Laravel worked really well. I was able get get to grips with setting up routes very easily and was impressed with how powerful they could be.

After a little playing around in the code I was able to very easily set up new urls and understand how they worked. I jumped straight in and created a couple of new views and using my routes I set up was able to see the pages without any problems.

Day 3 – Views and template whoas

Feeling confident with my quick success with the install and routes I was lured into a false sense of security for setting up my first page with my existing sites look and feel.

I wanted to set up the basic layout of my main site page broken into 3 parts. A. Header, a footer and the main content. Here is where the documentation started to fail me. The documentation only contains code snippets, which are good but when trying to pull everything together they don’t go together and there are gaps in how things are supposed to work. This showed itself in the nested views section.

After the initial read through of the documentation I started to look at views and nested views with partials. The idea being I have my main view and then nest the header and footer in them. This was a mistake, I could not fully get my head around how nesting was supposed to work and get my header and footer to show in my page. The code syntax provided worked without errors but all I ever saw was the main page, no headers and footers. Rereading the documentation I didn’t really understand the concept of a nested view. The explanation made sense but the implementation examples left me scratching my head and I still couldn’t get my headers and footers to show in my main page.

After some google searching I decided that nesting was the wrong approach. What I needed to do was focus on templates.

Day 4 – Templates and more whoas

I started out reading through the templates section in the larval docs and implemented a basic template. This worked great but reading further in the doc I discovered the Blade template system that comes with Laravel. This looked liked the solution I was really after.

I found a good example on NetTuts that went through the basics of setting up a Laravel site. I had already done a lot of the early steps so I skipped ahead to the section on Blade templates and implemented the example. I ran into my second road block here. I was getting no errors but I was seeing the raw template (all the tempting code) and not the HTML I should have been seeing. I kept switching back and forth between the tutorial on NetTuts and the Laravel documentation trying different things to get it to work. Finally after much tearing my hair out I found my problem, or rather a couple of problems.

Nettuts Laravel tutorial

Firstly I was cross mixing the 2 implementations. When I switched from using regular views to templates, in the code I no longer had to return the view from the controller. Instead I just needed to let the controller know which layout I wanted to use. When I switched to using the Blade templates, this needed to revert back however. I needed to return the view from the controller again and remove the layout information from the controller. This fixed my first problem.

My second problem one though was even trickier. I was able to get my HTML to show but in my HTML i was just seeing the calls to my variables (using the {{ varname }} ) notation, and not the values of the variables. Every documentation I read said this should work. Once again after playing around with the code I was able to find the problem. In my layout template I was including my headers and footers using the @include function. Variables would display fine in the main layout but anything pulled in though the @include would ignore the {{ varname }} calls. Reverting to raw php and echo for the variables in my @includes solved this problem.

Day 5 – CSS, links and images

Now I had my raw HTML working I could have left the various calls to CSS and my images as they were (normal HTML) and they would have worked fine. This project is about learning the Laravel framework though and one of the things the framework does is manage assets such as CSS and Javascript, and also auto creates the correct tags for links and images etc. A quick read of the Laravel documentation and within 30 minutes I had both styles and links up and running without any problems.

Day 6 – The heart if the site with models and database

Now that I had the look and feel of my site working it was time to get the content. I wanted to get the main homepage up and running, pulling in the content from the database. After spending most of the week struggling with the look and feel it was a pleasant change to find that getting into the business logic layer and database was very smooth and quick. Laravel makes it very easy to set up your database. Everything is taken care of, all you have to do it put in your database access details in and everything is set up.

Rather than opting to use raw database queries (which is possible with Laravel) I went with the query builder and was very pleased that I did. Within minutes I was able to very quickly replicate my raw queries from the old site with the new query builder language and it was much less coding. Passing the results to the view was easy and the Blade tempting language made displaying these results easy. I was really in the swing now and developing fast. Pulling together the main page (something that would normally take a few hours to build out the code) was done a lot quicker.

Day 7 – Awesome pagination

I had made good progress during the week and I accomplished my goal of getting to grips with the Laravel framework so that I can use it for future development but before I closed out the project for the week I wanted to try out one of the built in features provided by the framework, Pagination.

I have built pagination systems myself in the past and to get them right takes a lot of code. The demo and docs for Laravel claimed to be able to get pagination up and running with just a few lines of code so I wanted to give it a try. I picked on of the pages on my site that needed pagination and implemented the example code provided in the documentation. The code in itself was literally about 4 lines of code and on the first try without any extra effort I had pagination up and running just how I wanted it. I have to say I was very impressed and pleased with the framework.

End of the week

I can classify this week as a success. I accomplished what I wanted to, learnt a new framework and I’m well on my way to recoding one of my sites that needs it. The time spent in the early part of the week learning the basics was very well spent as subsequent tasks go much faster than they would if I was not using the framework. If you are into PHP coding and looking to learn something new I would recommend giving Laravel a try.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>