Basic Process of model, Controller, and view for Laravel5 framework Learning

Source: Internet
Author: User
This article describes the basic processes of models, controllers, and views in the Laravel5 framework. In fact, the MVC Architecture Model divides an interactive system into three components. The model contains core functions and data. View display to users

This article describes the basic processes of models, controllers, and views in the Laravel5 framework. In fact, the MVC Architecture Model divides an interactive system into three components. The model contains core functions and data. View display to users

Add route

The Code is as follows:


Route: get ('arties', 'articlescontroller @ Index ');

Create a controller

The Code is as follows:


Php artisan make: controller ArticlesController -- plain

Modify Controller

<? Php namespace App \ Http \ Controllers; use App \ Article; use App \ Http \ Requests; use App \ Http \ Controllers \ Controller; use Illuminate \ Http \ Request; class ArticlesController extends Controller {public function index () {$ articles = Article: all (); return $ articles ;}}

You can see the returned JSON result in the browser, cool!

Modify the controller and return to the view

Public function index () {$ articles = Article: all (); return view ('articles. Index', compact ('articles '));}

Create View

@ Extends ('layout ') @ section ('content') Articles @ foreach ($ articles as $ article) {{$ article-> title }}

{$ Article-> body }}

@ Endforeach @ stop

Browsing result, COOL !!!!

Show a single article

Add a route that displays detailed information

The Code is as follows:


Route: get ('articles/{id} ', 'articlescontroller @ show ');

Here, {id} is a parameter, indicating the id of the article to be displayed. Modify the controller:

Public function show ($ id) {$ article = Article: find ($ id); // if the document cannot be found if (is_null ($ article )) {// production environment APP_DEBUG = falseabort (404);} return view ('articles. show ', compact ('Article '));}

Laravel provides more convenient functions to modify the controller:

Public function show ($ id) {$ article = Article: findOrFail ($ id); return view ('articles. show', compact ('Article '));}

It's cool.

Create View

@ Extends ('layout ') @ section ('content') {$ article-> title }}{{ $ article-> body }}@ Stop

Try to access:/articles/1/articles/2 in the browser

Modify index View

@ Extends ('layout ') @ section ('content') Articles

@ Foreach ($ articles as $ article) {-- this method can be --} id} ">{{ $ article-> title }{{ -- this method is more flexible, unlimited path --}}
Id]) }}" >{{ $ article-> title }}{{ -- you can also use --}}
Id) }}" >{{ $ article-> title }}

{$ Article-> body }}

@ Endforeach @ stop

The above is all the content of this article, hoping to help you learn the Laravel5 framework.

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.