Laravel 5 Framework Learning Model, controller, view Foundation flow _php Instance

Source: Internet
Author: User
Add Route

Copy the Code code as follows:
Route::get (' artiles ', ' articlescontroller@index ');

Create a Controller

Copy the Code code as follows:
PHP Artisan Make:controller Articlescontroller--plain

Modifying the 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 = Artic Le::all ();    return $articles;  }}

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

Modify the controller to return to the view

Public Function Index () {    $articles = Article::all ();    Return view (' Articles.index ', compact (' articles '));  }

Create a View

@extends (' layout ') @section (' content ')  

Articles

@foreach ($articles as $article)

{{$article->title}}

{{$article->body}} @endforeach @stop

Browse results, cool!!!!

Display a single article

Add a route that shows detailed information

Copy the Code code as follows:
Route::get (' Articles/{id} ', ' articlescontroller@show ');

where {ID} is a parameter that represents the ID of the article to be displayed, modify the controller:

  Public function Show ($id) {    $article = Article::find ($id);    If you cannot find the article    if (Is_null ($article))    {      //Production environment App_debug=false      abort (404);    }    Return view (' Articles.show ', compact (' article '));  }

Laravel provides a more convenient function to modify the controller:

  Public function Show ($id) {    $article = Article::findorfail ($id);    Return view (' Articles.show ', compact (' article '));  }

It ' s cool.

New View

@extends (' layout ') @section (' content ')  

{{$article->title}}

{{$article->body}} @stop

Try access in the browser:/ARTICLES/1/ARTICLES/2

Modify the Index view

@extends (' layout ') @section (' content ')  

Articles

@foreach ($articles as $article)

{{--this way can--}} ID}} ">{{$article->title}} {{--this is more flexible and does not restrict the path--}}
ID]}} ">{{$article->title}} {{--can also be used--}}
ID)}} ">{{$article->title}}

{{$article->body}} @endforeach @stop

The above mentioned is the whole content of this article, I hope to be able to learn LARAVEL5 framework to help you.

  • 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.