Is a web craftsman with Laravel, MVC PHP framework

Is a web craftsman with Laravel , MVC PHP framework

Regardless of language or technology they are using, a global concept is: do not want to be worrying about infrastructure. This is where frameworks come. They help us and to streamline the development process, in an organized manner, avoiding duplication of code and more. The Laravel is an extremely productive PHP fremework with a big ecosystem, and is increasingly popular and used by the market.
Installation and first project

For starters, you can download and follow the step by step installation of the framework, which is very detailed on their website. The Laravel uses the Composer as internal dependencies manager, and therefore, you also need to install it.

Once installed, you can create new projects using the new laravel command-name design. An entire directory structure will be created, with Laravel and its required dependencies already installed. Cool, huh? This is one of the points that we like enough of the framework, you have to worry very little with infrastructure and settings, since it adopts a series of conventions to help you with this work. To create a project called store, simply run the command:

Laravel new store

Application text ready! Build something amazing will be displayed and ready , everything is set and ready to use. By accessing the store folder , you will see that there are various directories and configuration files. You can read more about this initial structure that link the documentation, but much of the work summary happen in the app folder, which focuses its controllers , models , and validatores etc.
Running Laravel with Artisan

To test our installation, we can use the command php artisan serves , that will run the application in PHP development server. Artisan is a command- line tool that comes with Laravel . During the post we see that it is much more than just up the server. After running this command , you can access your application at http: // localhos : 8000 . If all went well , a page with the logo of Laravel and a motivating phrase will be available :

Configuring HTTP verbs and routes

Configure routes with Laravel is extremely simple. There is one called routes.php file in the app directory \ Http , responsible for making this trabaho . An example mapping is:
Route :: get ( ‘ products ‘, ‘ ProductsController @ index’ ) ;

Thus we are teaching the Laravel that when someone makes a GET request to access the URI / products , the index method of our controller should be performed. The default is :
Route :: methodHttp ( ‘URI ‘, ‘ controllername @ function’ ) .

We can also register a new route POST type that points to the create method of our controller . Note that the URI will be the same .
Route :: post ( ‘ products ‘, ‘ @ ProductsController create’ ) ;.

The same can be done for other verbs like PUT, and DELETE PATCH but in these cases you need to pass a parameter _method with the HTTP method that severe use. An example would be :

<Input type = “hidden ” name = “_ method” value = ” DELETE “> .
Path stop and validation with regex

In the case of a search method, which should answer us with the details of a specific product , you must send via request a parameter with the id of the product. It is natural to do it right the URI , as in this example: / products / 1 . In this case , the product with ID 1 will be displayed. To do this, ie pass parameters by URI , we will :
Route :: get ( ‘ products / { id} ‘, ‘ ProductsController show @ ‘);

We may also use the where method , passes along a regular expression to validate the type and format of this parameter { id} .
Route :: get ( ‘ products / { id} ‘, ‘ ProductsController @ show’ ) -> where ( ‘id ‘, ‘ [ 0-9] + ‘);

Creating the first controller

We can create this ProdutoController manually , or using php artisan , which eliminates enough of boilerplate code as imports , class structure , etc. See how simple it is :
php artisan make : controller ProdutoController –plain .

The parameter indicates that -plain want an empty controller , otherwise it will create a number of methods commonly used in controllers – basically a CRUD . As a result of the command, the ProdutoController class will be created in app \ Http \ Controllers , as with the following content :
<? Php namespace App \ Http \ Controllers ;

use App \ Http \ Requests ;
use App \ Http \ Controllers \ Controller ;

use Illuminate \ Http \ Request ;

class ProdutoController extends Controller {
Implementing a product listing

Our first method will be the index , with the product listing . For now this method returns only one h1 , with the text ” Product List ” .
class ProdutoController extends Controller {

public function index ()
return ” < h1> Product List < / h1 >” ;
Candle to remember that this method has already been mapped on wheels file with the URI / products , so the access it will have the html with expected result :


This is already cool, but really do not want to see only a title , but a page with all products loaded from our database. This is where the model layer .
Creating a new model with artisan

We need to create a product model, which represent our database product table . To create a controller use the make artisan : controller , is not it? See how simple it is to create a model :
php artisan make: Product model

The product class and all its initial structure will be created :
<? Php namespace App ;

use Illuminate \ Database \ Eloquent \ Model ;

Product class extends Model {

Quick is easy. Every model (classes that inherit from Model , as in this case ) reflected in a table of our database , which by default will have the same class name , but with lower case and plural . That is, the product class accesses information from the Products table . Yes you can set up another table name by adding a property $ table in the class.

protected $ table = ‘ tableName ‘ ;

In addition to creating the model , we need a minimum of settings to allow our app in Laravel to communicate with the database. Simply access the database.php file and change the settings as in this example .
Discover the powerful Eloquent ORM

All ready , we can already use and abuse of the methods of database access the Model class. By the way, you must have realized that this class has a namespace with the word Eloquent , which is the framework name ORM used in Laravel . It makes our life simple by offering a series of declarative methods for the most different database operations. For example , to list all products in our index method , we will :

public function index ()
$ Product = Product :: all () ;
return $ product ;

See the method all who does all the magic , making this call in SQL statements that are executed on the bench. To test , note that we are now returning to the product list. If we access the path / products again , we will see a JSON with all the information of products persisted in the database .

In addition to all , there are several methods provided by Eloquent for many different operations with the database . Methods to save, update and certainly will find part of their daily lives . You can see these and other methods :
Working with views

Instead of a JSON , we want a view to be presented to the html of the product listing . To do this simply change the output of the method to view ( ‘ nomeDaSuaView ‘ ) , as in this example:

public function index ()
$ Product = Product :: all () ;
return view ( ‘ produto.lista ‘);

But beyond that, we want to make this list of available products for our HTML view, so we will use the method with :
public function index ()
$ Product = Product :: all () ;
return view ( ‘ produto.lista ‘ ) – > with ( “products” , $ products) ;

Note that the return is given as ‘ produto.lista ‘ , it means that the lista.php file will be present in a folder named product. The full path will be resources \ views \ product \ lista.php . This file will make a simple foreach , iterating the product arrays to mount the rows of a table:

< H1> Product List < / h1 >

<Table class = “table” >
< ? Php foreach ( $ products as $ product) { ? >
< Tr>
<Td > <? = $ Product- > name? > </ Td>
<Td > <? = $ Product- > description ?> < / Td>
<Td > <? = $ Product- > value ?> < / Td>
<Td > <? = $ Product- > amount ?> < / Td>
< / Tr>
< ? Php} ? >
< / Table>
Views more powerful with blade

The way that php offers information in the view may be a bit confused, quite verbose and we have to pollute the html with opening and closing braces. It is very easy to get confused, is not it? How about doing so:
@foreach ($ products as $ product)
<Td> {{$ product-> name}} </ td>
<Td> {{$ product-> description}} </ td>
<Td> {{$ product-> value}} </ td>
<Td> {{$ product-> amount}} </ td>
</ Tr>

See the change was little, but very significant. Read a @endforeach is more explanatory and easy to understand than a <\? Php}?>, Is not it? To use this syntax in your views, just add the blade length, which is the default template technology that framework. The file, named lista.php, will be called lista.blade.php. Just rename, without any additional configuration. And the blade goes far beyond that simplify the syntax, it opens the way to many other possibilities, such as using inheritance between views. To inherit the default view, which is automatically created along with the project, just make a @extends ( ‘app’) and add the content as follows:


<h1>Lista de Produtos</h1>

<table class=”table”>
@foreach ($produtos as $produto)

The content must be between @section ( ‘name’ ) and @stop . You can see more about blade , layout heritage and its extruturas control in :

When you run this code , we will have as a result a similar view with :

A multitude of features

You possibly noticed that the standard view of the menu, we have inherited in our list , there are already links to login and user registration . Not only is the link, these features have already been implemented for you ! Furthermore , a whole authentication mechanism is also already in place , ready for use. Cool, huh? At most you need to change one or another detail, depending on the needs of your project , but surely this basis it will be useful . These are just some of the many details that Laravel helps you , making it more productive and effective.

Want to know more about Laravel ? We write a book and online courses on the subject . Moreover, this is the framework used in our course of PHP and Object Orientation . And you have already used Laravel ? What other frameworks have used in your PHP projects?


Please enter your comment!
Please enter your name here