What is laravel, you might ask?
Laravel is “The PHP framework for web artisans” according to their webpage laravel.com. Essentially what this means is that for anyone that needs to create a new stable web-application of some sort, this is where you start.
You will get things like URL rewriting, database abstraction, form validation, HTML/graphics templating, MVC styled code separation for free without to much work.
How did I start using Laravel?
I used one of my old applications as a “test subject” for Laravel, where I would restart from the beginning and creating the same application, but with Laravel as a base instead of purely my own code.
I began with the simpelest of database structures that I could think of, almost the same as the old application. With Laravel, these are called Migrations. Each Migration will have an “up” and “down” function, which will do or reverse the changes to the database.
Laravel creates a table in the database to keep track of which migrations has been applied to the current database.
Then begins creation of models for some of the tables. The beautiful thing about these models is that you extend a class called Eloquent, which gives you some neat functionality out of the box. For example, if you have a user-table, which contains id, username, email and password you may use something like this:
$users = User::all(); return View::make('users')->with('users', $users);
This snippet could be used within a controller, or the “routes.php” in the “app/”-folder. It would query the user-table for all rows, and create one User-object for each row and send that as $users-variable to the view called “users”. The first parameter of the with()-method is the new variable name in the end, and the second one is the data that we would like to pass along.
The folder structure of Laravel
The folder structure would look something like this:
app commands config controllers database filters.php lang models routes.php start storage tests validators.php views artisan bootstrap composer.json composer.lock CONTRIBUTING.md phpunit.xml public css favicon.ico fonts index.php js packages robots.txt readme.md server.php vendor
The reason for me expanding the “app” and “public” folders is that that is where most of your work will be done.
In the “app”-folder, you have a folder called “config” where the “database.php”-file exists. This one should contain the authentication credentials for your database.
The “app/database”-folder will contain all migrations and seeds for your application. Seeds is the data that will be pre-populated in the database on installation of the application. For example, a administrator-account might be suitable for most applications having a user-table.
The “app/controllers” and “app/views” folders are two of the most important folders. This is where logic and design of your application should be stored.
The “routes.php” file will be where you describe the entry-points of your application as well as the links from URLs to controllers (logic).
Thoughts of Laravel
In the beginning, it was quite hard for me to reset my way of thinking since I was used to having one function-file that I include in the beginning of each entry point (could be many). I think that Laravel has forced me to learn how to code according to the MVC standard, which I now see is totally awesome. The reason for it being awesome is that the code is much more readable and changeable. There’s no longer a need to read the code multiple times to try to understand what it does.
It’s simple, each function/method in the controllers does one thing and one thing great.
Next web application I write will definitely use Laravel as a ground to stand on.