Installation
In order to install the Laravel framework, you can enter the following command at the terminal:
composer create-project laravel/laravel your-project-name
Alternatively, you can download the code base from Github. After installing Composer, run the command in the main directory of the project file composer install
, which will download and install the framework's dependent files.
After the installation framework is complete, browse through the project and familiarize yourself with the directory structure. app
directories contain views
, controllers
as well models
as folders. Most of the code for the application will be located in this directory. You may browse the app/config
directory, which contains the configuration options for the application.
Routing
As a start, let's create the first route. The simplest way to create a route in Laravel is to use a closure function. Open the app/routes.php
file and add the following route at the bottom of the file:
Route::get(‘users‘, function(){ return ‘Users!‘;});
Now, if you enter the route in the browser /users
, you will see the Users!
display as a response. Very good! You have created the first route.
A route can also be bound to a controller class, such as:
Route::get(‘users‘, ‘[email protected]‘);
This route tells the framework to /users
request a route with the UserController
function of the calling class getIndex
. For more information on controller routing, please visit the controller documentation.
Create a View
Below, we will create a simple attempt to display the user data. The view file is stored in the app/views
directory and contains the HTML of the app. We add two view files in this directory layout.blade.php
as well users.blade.php
. First create the layout.blade.php
file:
Next, create the users.blade.php
view:
@extends(‘layout‘)@section(‘content‘) Users!@stop
You may think some of the syntax inside is very strange. That's because we're using Laravel's templating system: Blade. Blade is very fast because you only need to convert some of the regular expressions in the template to pure PHP. Blade provides powerful features such as template inheritance, as well as syntax sugars for some typical control structures such as if
and for
provided. For more information, please visit the Blade documentation.
Now that we have created the view, let's return to the /users
route and let it return the contents of the view file:
Route::get(‘users‘, function(){ return View::make(‘users‘);});
Very good! Now that we have built a simple view from the layout extension, below we will start using the database.
Create a migrationIn order to create a table to save our data, we will use the Laravel migration system. The migration system allows us to define changes to the database in an expressive manner and is easily shared with other members of the team.
First, let's configure the connection to the database. You can app/config/database.php
configure connections for all databases in a file. By default, Laravel uses SQLite, and the SQLite database is saved in the app/database
directory, and if you prefer, you can also change the driver
connection authentication parameters that are selected for mysql
and set in the configuration file mysql
.
Below, in order to create the migration, we will use the Artisan command line. Switch to the root directory of the project and execute the following command at the terminal:
php artisan migrate:make create_users_table
Below, app/database/migrations
Locate the resulting migration file under the directory. This file contains a class in which two functions are defined: up
as well down
. up
make the changes you want to the database in the method, and undo the changes in the down
method.
Let's define a migration as follows:
public function up(){ Schema::create(‘users‘, function($table) { $table->increments(‘id‘); $table->string(‘email‘)->unique(); $table->string(‘name‘); $table->timestamps(); });}public function down(){ Schema::drop(‘users‘);}
Below, we can use commands on the terminal to migrate
perform the above migration. Run the following command at the root of the project:
php artisan migrate
If you want to roll back a migration, you can follow the migrate:rollback
commands. Now that we have the table for the database, let's start populating some data!
Eloquent ORMLaravel has an excellent orm:eloquent. If you've used the Ruby on Rails framework before, you'll find eloquent is similar to it because it adheres to the ActiveRecord ORM pattern of database interaction.
First, let's top a model, a eloquent model that can be used to query a table for a related database, or to represent a row in a table. Don't worry, you'll get to the bottom of it soon. Model files are generally saved in the app/models
directory. Let's define a model like this User.php
:
class User extends Eloquent {}
Note that we do not need to tell eloquent which table was used. Eloquent has many booking conventions, one of which is the plural of the model name as the table name in the model's database. Very convenient!
Insert some data into the table using your favorite database administrator tool users
, and we'll use eloquent to get the data and pass it to the view.
Now we modify the route as follows /users
:
Route::get(‘users‘, function(){ $users = User::all(); return View::make(‘users‘)->with(‘users‘, $users);});
Let's take a look at this route. First, User
the method of the model all
gets users
all the data in the table. We then with
pass the records to the view through the function. with
method accepts a name and a value that is used to pass the available data to the view.
That's amazing. Below we will show these users in the view.
Show dataNow we have passed the variable in the view users
. We can show them like this:
@extends(‘layout‘)@section(‘content‘) @foreach($users as $user) <p>{{ $user->name }}</p> @endforeach@stop
You may want to know where to find the echo
expression. When using the Blade template, you can display it by enclosing the data in two curly braces. Now you are able to access the /users
route and see the user's name displayed on the screen.
It's just a start. In this guide, you already know some of the basics of Laravel, but there are a lot more exciting things to learn, please continue to read the documentation, and get a deeper understanding of eloquent and Blade. Or maybe you're more interested in queue and unit testing, or if you want to refactor through the IoC container, it all depends on you.
Laravel Quick Guide