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. The view displays information to the user. The controller processes user input. A view and a controller constitute a user interface.
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 the 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 = false abort (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.