Learn Laravel with me Quick start _php instances

Source: Internet
Author: User
Tags composer install ruby on rails
installation

The Laravel framework uses Composer to perform installation and dependency management. If you haven't installed it yet, start installing Composer now.

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

Composer Create-project Laravel/laravel Your-project-name
Alternatively, you can download it from the GitHub repository. Next, after installing composer, execute the composer install command under the project root directory. The command will download and install the framework's dependent components.

Write permissions

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

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

Routing

We started to create our first route. In Laravel, the method of simple routing is closure. Open the app/routes.php file by adding the following code:

Route::get (' Users ', function ()
{
Return ' users! ';
});
Now that you enter/users in your Web browser, you should see users! Output. That's great! Your first route has been created.

Routing can also be given to the Controller class. For example:

Route::get (' Users ', ' usercontroller@getindex ');
The route tells the framework that the/users routing request should call the GetIndex method of the Usercontroller class. To see more information about routing controllers, 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 create the layout.blade.php file first:

Copy the Code code as follows:


Laravel Quickstart

@yield (' content ')


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

Copy the Code code as follows:
@extends (' layout ')

@section (' content ')
users!
@stop

The syntax here may make you feel unfamiliar. Because we are using the Laravel template system: Blade. 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 the Blade documentation for more information.

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

Copy the 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 tier.

Create a migration

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

First, we configure the database connection. You can configure all 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 the 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 the 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 make a change to the database table, and in the down method you just need to remove it.

Let's define the following migration:

Copy the 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 the 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

The Laravel offers great orm:eloquent. If you've 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, as well as 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 the 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 a database table that uses the plural form of the model as the model. Very convenient!

Using your favorite database management tools, insert a few rows 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 the Code code as follows:
Route::get (' Users ', function ()
{
$users = User::all ();

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

Let's take a look at the route. First, the all method of the User model 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.

I'm excited. Now we are ready to display the user in our view!

Show data

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

Copy the Code code as follows:
@extends (' layout ')

@section (' content ')
@foreach ($users as $user)

{{$user->name}}


@endforeach
@stop

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

This is just the beginning. In this series of tutorials, you've learned about the basics of Laravel, but there are more exciting things to learn. Continue reading the document and drill down into eloquent and blade these powerful features. Or you are interested in queue and unit tests. Perhaps you want to know the IOC Container, the choice is yours!

  • 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.