Laravel 5 Series Tutorial II: Routing, view, controller workflow

Source: Internet
Author: User

Free Video Tutorial Address Https://laravist.com/series/laravel-5-basic

Previous tutorial We walked so long road, finally put Laravel installed, this tutorial we will enter the magical world of laravel, mainly to explain the laravel of router,views,controllers work flow, The goal is to get people to understand how Laravel works when dealing with a GET request.

Before we start, we first have to start up our servers, and if you use Laravel's artisan, you can directly:

php artisan serve

Then visit localhost:8000, and you'll see Laravel.

I still use homestead here:

homestead up

After the homestead starts, we can start writing the code happily.

Laravel file Directory

Then visit our previous section of the domain name set: Blog.dev, you can also see our laravel. If you encounter any problems when installing or using homestead, you can ask me directly.

First of all, for a novice to contact Laravel, you may be laravel of the file directory feel very confused, because it is too many things, in the end what is console,events,providers ... A lot of stuff is here.

But don't worry, our follow-up tutorials will continue to explain the relevant content.

At present, we are concerned about http/This folder, you can simply remember that this folder is used to come out of HTTP requests, such as when we visit Blog.dev, Laravel is how the lovely page response to us? This can be app/Http/routes.php found in this file, as shown:

Route::get(‘/‘, function () {    return view(‘welcome‘);});

What does that mean? In fact, here you can understand: we register such a route, when the user sends a GET request (简单理解就是在浏览器地址栏中访问) , what kind of Get request? What kind of browsing address? Well, that is the root directory of the site, that is, when a user visits our site root directory, we should give users what kind of response? Here we are directly executing a function function () {} , which is responsible for loading a view return view(‘welcome‘); , the attempt file is located resources/views/welcome.blade.php , Here the attempt to file suffix blade.php may be confused, because Laravel uses the blade template engine, if you touch other template engine such as twig,smarty, the understanding of the template engine may be a little clearer. But if you are still not quite clear here, there is no relationship, I will follow a special course to introduce the blade template. Now you need to keep in mind that the Laravel template file is blade.php end.

You can directly open resources/views/welcome.blade.php this file to see, inside is actually some HTML code, easy enough.

So the above route registration is actually responsible for resolving the response when accessing Blog.dev. So let's try a Hello World simple return:

Route::get(‘/‘, function () {    return ‘Hello World‘;// return view(‘welcome‘);});

Go back to the browser and visit Blog.dev again, and we'll see the Hello World written above.

Parameters

Trying to pass in the parameters, we re-register a route:

Route::get(‘/‘, function () {    return view(‘welcome‘);});Route::get(‘user/{name}‘, function ($name) { return ‘Hello ‘.$name;});

The second above is our own re-registration of the route, in the route the variable is wrapped in curly braces {} , and then receive this parameter in the processing function, access to Http://blog.dev/user/guys, you can see the following return results.

Above this time, if we visit Http://blog.dev/user, this is an error, because this route expects you to enter a variable $name, but you do not, so will error, we can use optional parameters to achieve this purpose

Optional Parameters:
Route::get(‘user/{name?}‘, function ($name = null) { return ‘Hello ‘.$name;});

This time, again visit Http://blog.dev/user will not error, but it seems that this time only return Hello, we can also set a default value:

Route::get(‘user/{name?}‘, function ($name = ‘JellyBool‘) { return ‘Hello ‘.$name;});

This time, if we do not enter $name This variable, we will returnHello JellyBool

Using the Controller

These routes registered above are directly using anonymous functions to return strings or load views to us, but where does our controller go? How do we use the controller (controllers)? First we need to understand that in registering the route is using the controller in Laravel is probably written like this:

Route::get(‘/‘,‘[email protected]‘);

We delete the original registration route in the routes.php, leaving only the above line of code: This line of code means that when the user requests our site and directory, I need to find ArticleController and execute its index method, then we need to create our ArticleController :

Execute at the command line:

php artisan make:controller ArticleController

The previous article said artisan This tool is very powerful, he can be used to generate a lot of things we need in the development, to our development brought a lot of convenience, if you take a little time to adapt, I am sure, you will love Laravel, will love artisan. Here is just the use of the artisan make:controller command, in fact, artisan can do a lot of things, we in this tutorial will come into contact, if you can't wait, you can directly see the official documents:

Http://laravel.com/docs/5.1/artisan

The above command gives us a app/Http/Controllers/ build in the directory ArticleController.php , we open it to see:

In this file, Laravel helped us generate a bunch of methods: index() , show() , create() ... Wait, and then we just routes.php registered the route in router (file) to use ArticleController the index method, we will load a view inside the index () method:

public function index() { return view(‘articles.lists‘); }
Create a View file

This view file should be located resources/views/articles/lists.blade.php , but in the Views folder, we do not have articles/ this folder and lists.blade.php so we need to create it manually.

It is important to note that the Laravel view() method will default from the Views folder to find the view file, so you do not have to view() add this path in the method resources/views , and you do not have to blade.php write, Laravel will automatically handle these things, as for the views() method articles.lists . You can use a path alias in the /articles/lists

After creating the articles/lists.blade.php simple HTML code after writing:

<! DOCTYPE html><html xmlns= http://www.w3.org/ 1999/html "><head> <meta charset= "UTF-8" > <title>article lists Page</title> </head> <body>< h1> this is the article List page </h1> </body></ HTML>              

Then visit blog.dev/and we can see the above template content:

Here, do not know you to Laravel, Router Views , Controllers These three work flow has not a clear understanding, usually we need to implement a function (or new Create a page) is this:

---> 2. 创建对于的控制器 ---> 3. 在控制器中得对于方法加载视图

This corresponds to the actual operation, presumably:

Route::get(‘/‘,‘[email protected]‘);2. php artisan make:controller ArticleController3. public function index() { return view(‘articles.lists‘); }
Next section

About Laravel's router,views,controllers workflow is about here, if you have any questions, you can always ask me in the comments. Then the next section will say the following content:

    1. Configuration of the database and migration usage of laravel
    2. To pass a variable to a view file
    3. Basic usage of blade templates

Laravel 5 Series Tutorial II: Routing, view, controller workflow

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.