Access all tutorials in sprocket icon.
September 25, 2015 from author Bill Keck.
With so many choices out there about which PHP framework to choose, it can be especially confusing for beginners or those who have never worked with PHP frameworks to figure out which framework merits their time. And right along with that thought comes the question of which was is easiest to learn?
The last question supposes that there is anything easy about learning programming, which, sorry to say, is typically not the case. That said, I can tell you from personal experience that Laravel 5.1 is easier for me to pick up than other frameworks I’ve worked with.
Now that could be because I’m further along on my programming journey than I was when I worked with other frameworks or even no framework. No doubt that plays a role in the ability to pick things up quickly.
That said, I do think there are at least 10 big reasons why Laravel shines as a framework. I’ll list the reasons first, then go over each one:
Top Ten Reasons To Use Laravel:
- Excellent Documentation.
- Intuitive Syntax
- Practical Application Structure
- Artisan Code Generation
- Out-of-the-box User Model
- Blade Templating Engine
- Dependency Injection Made Simple
- Supporting Products and Packages
- Innovative Founder
1. Excellent Documentation
Taylor Otwell, the founder and main developer of Laravel has written the documentation of the framework himself. One of the interesting things about Taylor, which we will see play out in some of the other reasons I’m listing, is that he has an uncanny command of syntax, both in the English language and in programming.
He seems to have an intuitive knack for making things simpler and more digestible, organized in a way that the average programmer can easily consume. His documentation and attention to detail reflect this.
Before I start working with a framework, I read the docs from front to back, before I write a single line a code. That is, I read them if they are readable or understandable, which often they are not. In those cases, I just usually get stuck and give up.
Anyway, the value of great documentation can’t be understated. Here’s a trivial example. I decided to add a checkbox to my registration form to have users accept terms of service. Sounds like a common scenario, does it not?
Now imagine my surprise when I found out that Safari does not support the HTML 5 required tag. No problem, I just need to handle it through Laravel’s validation. So let’s see, the check box will be null or empty string if not checked, err, is that right? Hmm. I wonder what kind of validation Laravel offers to account for this. It would have to be looking for a boolean, right? But then do I have to convert it to a 0 to get it work?
Well, fortunately, the docs on validation are crystal clear and include a section listing all the available out-of-the-box validators. The very first one is the accepted validator. The docs describe it as:
The field under validation must be yes, on, 1, or true.
This is useful for validating "Terms of Service" acceptance.
It just can’t get any easier or more clear than that. Notice how he anticipated the use for the validator. Now you might say, wow, that’s just too simple a reason to love a framework, but you have to keep in mind this is a consistent experience across all the docs. I might have to refer to the docs to do a left join, for example, because I just can’t remember everything off the top of my head. Luckily, the docs are just as simple and crystal clear on that too.
Laravel is supported by Laracasts, which features a mix of free and paid videos that show you how to use Laravel. The videos are all done by Jeffery Way, who is an expert and well-versed instructor. He seems to have his finger on the pulse of the essentials and offers clear and concise programming instruction. The production quality is high, and the lessons are well-thought out and meaningful.
I can’t really imagine trying to learn PHP and Laravel without subscribing to Laracasts. When a new feature like login throttling comes out, which came out relatively recently, Laracasts always follows with a tutorial.
In many ways, I think Laracasts is a huge competitive advantage for Laravel, because even though it costs money, it supports the framework on level that I have not seen in any other php framework.
My only wish for Laracasts is that they would do more videos. Some episodes on my wishlist: Dynamic and complex forms, Creating a Cart, Query Writing 101, Advanced Query Writing, Writing Queries Like A Rocket Scientist, How To Get Mysql To Dance And Sing… Ah, you get the idea.
Anyway, Laracasts is addicting. It’s kind of like watching your favorite show, you just want more episodes. In this case, watching more episodes means acquiring more skills, which theoretically gives you more earnings potential, so this is one of those pleasurable experiences in life that actually can get you paid.
3. Intuitive Syntax
Robert C. Martin wrote a universally acclaimed book named Clean Code. In the book, Martin shows us how to write code that is descriptive and focused to the task, leading to clearer and more maintainable code.
Taylor Otwell has adopted his principles with Laravel and it just seems to reflect in every nuance of the framework. For example, there is a trait that the AuthController uses named RegistersUsers. Anyone want to guess what that trait does? Well it has 2 methods. One shows the registration form and the other processes the registration.
There are just so many examples of perfect syntax, I can’t even really begin to relate this, but here’s another for example:
$roles = Role::paginate(5);
In this case, we are asking the Role model for all roles, with pagination set to 5 results per page. That’s how simple basic pagination is in Laravel. There’s one additional tag needed in the view, and that’s it. Super simple and super easy to understand.
When I moved into Laravel, I kept thinking, it can’t be this simple…
Ok, one last example:
$role = Role::findOrFail($id);
Here we are using findOrFail to return a single model instance. If it doesn’t get a result, it throws a ModelNotFoundException, which you can then handle with a pretty view page. See my tutorial on exception handling for that. Again, findOrFail is awesome in it’s simplicity and clarity.
Taylor’s feel for syntax, both in English and in PHP, makes Laravel the most accessible PHP framework that I have ever worked with or even attempted to work with.
4. Practical Application Structure
This section was a little difficult to create a title for. The application structure is actually more complex in some ways, but ironically, this makes it easier to work with. So I just think it’s a practical approach.
Years ago, Taylor Otwell described the shortfall of the concept of the model in the mvc pattern in his book, which, incidentally, is not really a book for beginners. When I read it years ago, I was clueless. I reread it not too long ago, not hard because it’s 69 pages in length, and it reads like prediction of what he planned for Laravel 5.
In Laravel 5, he deconstructs the model into more discrete pieces. We get many more folders, which, when I first saw them, made my head explode. I thought it was way too complex, way too much to take in. How dare he make my model folder disappear…
But then as I started using it, I found that while I sometimes had to hunt for the folder, I was able to think in terms of the Laravel application structure much more easily than I could with other frameworks. The organizational sense to it is intuitive, once you get used to it.
For example, it has a folder for Requests, which is in the HTTP folder along with a Controllers folder, routes, a Middleware folder and more. You can’t really just grasp it in one quick glance.
On the other hand, the Requests folder provides the perfect setting for handling validation requests, and you’ll find after using it that it’s very intuitive to think of if that way.
The Middleware folder contains classes for processing the request that act like discrete filters, so for example, the Authenticate class will require the user to be logged in. You can call it on the constructor of the Controller like so:
public function __construct()
What that does is call the auth middleware, which will require the user be logged in. The constructor calls it, then it returns true or false, true if the user is logged in. If not they are denied access. It’s just so simple.
So this is an innovative approach, and at first it seemed a little harder to grasp, but once you get it, you see just how cool it is, and it’s easy to work with. It gives you an incredible amount of flexibility.
One of the finest points of the Laravel Application Structure is the app.php file that sits in the Config folder. This is where you add service providers to the application, and you will do this when you add packages or create your own service providers.
Service providers are autoloaded by the framework so you have access to the classes. In the aliases array, you provide references to the classes so you can access them via a facade, which is just a map to a particular class. This can sound complicated, but in practice, it is very easy to work with.
Like I said, I can’t call the application structure simple, because it has a lot of different pieces to it that I haven’t seen elsewhere, but it is easy to work with, and it ends up being more intuitive because of that. To me, that means it is a very practical structure.
5. Artisan Code Generation
Laravel ships with a great command line tool named Artisan. Need to make a controller? Try this on your command line:
php artisan make:controller SampleController
Just hit enter and you’ve got a controller stubbed out for you, and located in the correct folder. If you want to make a request class to hold some validation for a form input, perhaps on a profile model, try:
php artisan make:request ProfileRequest
And that will appear in your Requests folder for you, stubbed out, namespaced and ready to code.
Migrations are a snap too:
php artisan make:migration add_is_admin_to_users_table
That will create a migration in the migrations folder.
And on that note, tying back to the importance of well-written documentation, the docs cover all the migration commands, so you are never lost.
I had a lot of resistance to working with migrations. I thought they were a cheeseball approach to data-modeling. But when I started working with Laravel, I decided to follow along with the work flow as closely as possible. Once I got used to creating migrations, I realized they were pretty simple to work with, and you get added benefits. When you collaborate on projects, remote team members can run the migrations and have the same exact DB structure, which is great way to manage that. It really is so easy to work with, you will love it.
6. Out-of-the-box User Model
Something I was glad to see out-of-the-box in Laravel 5 is the user model. You get the controllers and migrations that you need to setup the users table and a fully working user registration and login system, with forgot password functionality.
Laravel does require you to write the routes and views on your own, and I provide a handy tutorial for that purpose.
Having a ready-made user model let’s you get your projects up and running quickly and it’s much appreciated. It’s also nice to have the forgot password functionality there because that involves setting tokens that expire, which can be fairly complicated and time-consuming to setup. It’s cool to have this done before you even start.
7. Blade Templating Engine
The Blade templating engine is so intuitive and makes working with the typical PHP/HTML spaghetti so much better, that’s it one of the best features of the framework. If you ever have had to chop up an if statement with HTML inside of it, you know exactly what I mean. But with blade, it’s almost effortless:
//show the html you want for when the user is logged in.
// show other html when the user is not logged in.
Blade compiles everything, so you don’t incur any overhead for using the template engine and that is just wonderful. Again the syntax is just perfection. Work with it and you’ll see what I mean.
8. Dependency Injection Made Simple
If you been coding in PHP in the last few years, you’ve no doubt heard a lot of buzz about dependency injection. Although it sounds complicated, it’s actually pretty simple in the context of Laravel.
Dependency injection allows you to code to a contract instead of a concrete class, the benefit being that you can easily swap out implementations with minimal impact on the code. It is centered on this idea of loose-coupling of classes which makes your code more extensible and easier to maintain.
You can look at the app.php file in config as an example of loose-coupling. You can easily swap out service providers, which are the building blocks of the application.
Ok, so when I first heard about this, it all seemed so gratuitous to me. I didn’t quite understand the benefits of contracts, which are interfaces, and the concept of loose coupling. I felt like it made no sense for medium to smaller size applications by adding unnecessary complication. It just seemed like overkill. I was really wrong about that.
Laravel adheres to these principles in a very intuitive way, and rather than create convolution, it actually makes things simpler. The implementation is somehow simpler than explaining the concept in detail. You can check out my tutorial on creating a service provider, if you want to see an implementation that will help you understand it better.
Laravel also supports method injection, which is when you type hint a class in the method signature like so:
public function show(Request $request, $id)
$input = $request->all();
So in the above, Request type hints an instance of the Request class, which means we have now have an instantiated instance named $request, and this is typically what you are going to see for capturing form data. Also in the above, you can see we are grabbing all the form input and assigning it to a variable named $input. Look at how concise and intuitive that is.
This is a top ten list, not a tutorial, so I won’t take it further, but I think you get the idea of how Laravel is doing innovative things. When you work with it, you’ll find it’s really not that complicated after all.
9. Supporting Products and Packages
Laravel is well-supported by many packages that will help you move your project along quickly. I wrote a top ten list of Laravel packages, so I won’t repeat that here.
In addition to all that, Taylor Otwell has brought out a number of products in the Laravel family that combine into a very powerful suite of products and services. These include Lumen, Elixer, Forge, Homestead, and Envoyer. These are products that will help you in numerous ways, everything from local environment (Homestead) to deployment (Forge, Envoyer). And I should mention again that Laracasts as an educational support component is just phenomenal. Our company purchased a company license, so all of our developers can have access to it. It’s well worth the money.
In some ways, Laravel is more like a commercial product than an open source library and I say that as a strength, not a weakness. It has the polish and the support to make it easy to work with. Maybe I should say easier. I don’t want to give the impression that everything is easy, because as we all know, programming is not easy. That said, Laravel takes you as close to the promised land of coder happiness as I have ever seen with PHP.
10. Innovative Founder
I’ve never met Taylor Otwell. But I have watched Laravel since 2012 and I have seen it go from the wobbly new kid on the block to the dominant PHP framework. It’s not an accident. Taylor is the driving force behind the framework and continually pushes out innovative products.
A couple of examples are Lumen, an uber-fast micro-framework and Laravel Spark, a SAAS template built in Laravel now in alpha. And the features in Laravel continue to evolve as well.
On that note, I learned a lesson recently. I decided to simplify the AuthController and bypass the auth traits, just to make it easier for me to work on things like login and registration. Looking back, it wasn’t really easier, but that is what I thought at the time. The right word is I made it more familiar, not easier. And of course I shared this via tutorial.
Then 2 months after writing the tutorial, I noticed that people were saying the code was broken. How could that be? Well, it turns out Taylor released an update, and as part of that update, he changed some of the underlying code to allow for a new throttles logins feature, which built in a defense against bots attacking the login. That’s a great feature.
Because I had moved away from the framework code, I couldn’t take advantage of it. And my code was broken. So I decided to rewrite the tutorial.
This time I stuck to the framework code. If I want to change something, I’ll pull the method up to the AuthController and overwrite it, that way I don’t mess with the framework code. This will make it easier for me to adapt to changes, and of course take advantage of new features as they are released.
This way I can ride the lightning of Taylor’s innovation. It’s a great thing and it is having a huge and positive influence on the PHP community. Coding in PHP is becoming easier thanks to Taylor Otwell, it’s that simple.
In conclusion, my personal experience with Laravel is that it has simultaneously made me a better programmer and made it simpler to write code. If I sound like an enthusiastic supporter of Laravel, I am. It’s made my coding life more productive and enjoyable and that’s a wonderful thing to get from a free PHP framework.
I hope you have enjoyed this tutorial and found it useful. Click on the sprocket icon at the top of the page to see all tutorials. Please comment, share, and like if you can, thanks.
I don’t have a donate button, but If you would like to support my work and learn more about Laravel, you can do so by buying one of my books, Laraboot: laravel 5* For Beginners, I really appreciate it.