Laravel5 framework: Model, controller, basic view process, and laravel framework. The basic process of Laravel5 framework learning model, controller, and view. the code for adding a Route copy in the laravel framework is as follows: Route: get (artiles, ArticlesController @ index ); create and control basic processes for model, controller, and view of Laravel 5 framework learning. laravel framework
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.
Http://www.bkjia.com/PHPjc/980212.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/980212.htmlTechArticleLaravel 5 framework learning model, controller, view basic flow, laravel framework to add routing code: Route: get ('artiles ', 'articlescontroller @ index '); create Control...