Basic process of model, controller, and view for Laravel5 framework learning, laravel framework _ PHP Tutorial

Source: Internet
Author: User
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...

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.