Follow me to learn Laravel's quick start _php instance

Source: Internet
Author: User
Tags documentation sqlite sqlite database composer install laravel migration ruby on rails

Installation

The Laravel framework uses Composer to perform installation and dependency management. If it's not installed, start installing Composer now.

After installing composer, you can install the laravel using the command line with the following command:

Composer Create-project Laravel/laravel Your-project-name
Alternatively, you can download it from the GitHub warehouse. Next, after you install composer, execute the composer install command in the project root directory. This command will download and install the dependent components of the framework.

Write permission

After installing Laravel, you also need to set the Write permission for the App/storage directory for the Web server. Please refer to the installation section for more information on configuration.

Directory structure

After you install the frame, you need to familiarize yourself with the directory structure of the project. The app folder contains some such as views, controllers, and models directories. Most of the code in the program will be stored in these directories. You can also check out some of the configuration items in the App/config folder.

Routing

We started to create our first route. In Laravel, the simple routing approach is closure. Open the app/routes.php file and add the following code:

Route::get (' Users ', function ()
{
Return ' users! ';
});
Now that you're typing/users in a Web browser, you should see users! Output. That's great! You have created your first route.

A route can also be assigned to a controller class. For example:

Route::get (' Users ', ' usercontroller@getindex ');
This route tells the framework that the/users routing request should call the GetIndex method of the Usercontroller class. To see more about routing controller information, view the controller documentation.

Create a View

Next, we'll create a view to display our user data. The view is stored in the App/views folder in HTML code. We will store two view files to this folder: Layout.blade.php and users.blade.php. First, let's first create the layout.blade.php file:

Copy Code code as follows:

<body>

@yield (' content ')
</body>

Next, we create the users.blade.php view:

Copy Code code as follows:

@extends (' layout ')

@section (' content ')
users!
@stop

The grammar here may make you feel strange. Because we are using the Laravel template system: Blade. The Blade is very fast, because only a small number of regular expressions are used to compile your template into the original PHP code. Blade provides powerful features such as template inheritance, as well as some commonly used PHP control structure syntax sugars, such as if and for. See Blade documentation for more information.

Now that we have our view, let's return to the/users route. We use views instead of returning users!:

Copy Code code as follows:

Route::get (' Users ', function ()
{
Return View::make (' users ');
});

Pretty! Now you have successfully created a view that inherits to layout. Next, let's start the database layer.

Create a migration

To create a table to hold our data, we will use the Laravel migration system. Migration describes the changes in the database, which makes it easy to share with their team members.

First, we configure the database connection. You can configure all the database connection information in the app/config/database.php file. By default, Laravel is configured to use SQLite, and a SQLite database is stored in the App/database directory. You can modify the driver option of the database configuration file to MySQL and configure MySQL connection information.

Next, to create the migration, we will use the Artisan CLI. In the project root directory, execute the following command in the terminal:

Copy Code code as follows:

PHP Artisan Migrate:make create_users_table

Then, locate the generated migration file app/database/migrations directory. The file contains a class that contains two methods: up and down. In the up method, you want to nominate the database table modification, in the down method you only need to remove it.

Let's define the following migrations:

Copy Code code 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::d rop (' users ');
}

We then use the terminal to run the Migrate command in the project root directory to perform the migration:

Copy Code code as follows:

PHP Artisan Migrate

If you want to roll back the migration, you can execute the migrate:rollback command. Now that we have a database table, let's add some data!

Eloquent ORM

Laravel provides a great orm:eloquent. If you've ever used the Ruby on Rails framework, you'll find eloquent very similar because it follows the ActiveRecord ORM style of database interaction.

First, let's define a model. The eloquent model can be used to query related data tables, and a row within a table. Don't worry, we'll talk about it soon! Models are usually stored in the App/models directory. Let's define a user.php model in this directory, such as:

Copy Code code as follows:

Class User extends eloquent {}

Note that we did not tell eloquent which table to use. Eloquent has many conventions, one is the database table that uses the plural form of the model as the model. Very convenient!

Using your favorite database management tools, insert a few lines of data into the users table, and we'll use eloquent to get them and pass them to the view.

Now we modify our/users route as follows:

Copy Code code as follows:

Route::get (' Users ', function ()
{
$users = User::all ();

Return View::make (' users ')->with (' users ', $users);
});

Let's take a look at that route. First, the User model's all method will get all the records from the Users table. Next, we pass these records to the view through the With method. The With method accepts a key and a value, and the value can be used in the view.

It's exciting. Now we are ready to display the user in our view!

Display data

Now that we have access to the users class in our view, we can show them as follows:

Copy Code code as follows:

@extends (' layout ')

@section (' content ')
@foreach ($users as $user)
<p>{{$user->name}}</p>
@endforeach
@stop

You can see that the Echo statement was not found. When using a Blade, you can use two curly braces to output the data. Very simply, you should now be able to view the user name as the response output through the/users path.

This is just the beginning. In this series of tutorials, you've learned the basics of laravel, but there are more exciting things to learn. Continue reading the document and delve into the powerful features of eloquent and blades. Or you're interested in queues and unit tests. Maybe you want to know about the IOC Container, the choice lies with you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.