Introducing Laravel, part 1

Oct 21, 2013
PHP
By

Laravel is a PHP Web Application framework that works with the MVC pattern, as its website states: it’s the PHP framework for web artisans.

Version 1.0 of Laravel was released just a couple of years ago and this PHP framework has definitely taken the community by storm with its simplicity and maintainability. Laravel makes creating web applications simple whilst also being easy to learn.

Laravel works in a very focused way, allowing you to concentrate on what’s important for your application and structuring that in the best way possible. And if you think about it, that’s the whole idea of an MVC framework: having it do the boring tasks while you focus on making your app the best it can be.

In this article series we’ll take a look at the basics of this framework and discover what all the fuss is about.

Set Up

The first thing you need to do after installing Laravel is to configure your mySQL settings in app/config/database.php, the lines to edit are:

'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),

Configuring this is the first thing you should think about when starting a new Laravel application.

Simple Routing

Routing is what redirects the browser to your application correctly and Laravel takes advantage of this in a great way. Let’s say you want to reach ‘yourwebsite.com/sample-page’, you would have to create a router to successfully show what you wanted the user to see when he arrives at that page. To do that you need to first open app/routes.php and add a route for that page, like so:

Route::get('sample-page', function() {
return: ‘My Sample Page’;
});

Routes are declared using the Route class and then using the get method. Laravel catches the HTTP requests and when it sees that the request has been fulfilled correctly it runs the function inside it, which in this case only returns a string.

Most of the time that isn’t what we want to show on a page, we usually want to show a view…

Views

If you are familiar with the MVC pattern you are also familiar with views. In essence a view is what the user sees, an HTML template that is returned to the browser and these views typically don’t contain massive amounts of logic, just simple if and for each statements.

To create our sample view we need to navigate to app/views and create a new php file called sample.php:

<!doctype html>
<html lang="en">
<head>
<title>Sample page</title>`</head>
<body>
<h1> This page has a heading</h1>
<p>And also some paragraph text</p>
</body>
</html>

Now that view is done we need to go back to our routes and call this view when the user is at yourwebsite.com/sample-page and for that we need to make a view:

Route::get('sample-page', function(){
return View::make('sample');
});

When using view::make() we create a new instance of the view object and pass it the name we want to use as its parameter. If you try now you will see our view showing up on the page.

Views can be made using standard PHP but to take full advantage of Laravel I advise you to use the built-in template engine called blade, it creates much cleaner and simpler code and it’s definitely something to look up.

This sums up the first part of this Laravel introduction, in the next part we will look at Models and of course, Controllers, to give us a better understanding of how they work in the framework.

 

Have you developed with Laravel? What are its most exciting features? Let us know in the comments.

Author: Sara Vieira
Sara Vieira is a freelance Web Designer and Developer with a passion for HTML5/CSS3 and jQuery. You can follow her on twitter or check out her website.
  • kaning

    My two favourite features are the IoC container / Dependency injection and also the Eloquent ORM.

  • http://www.parajuegos.info/ Para Juegos

    Technology is increasingly developing its version also changes based on the latter always modern and better than before.