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.