Laravel 5 Framework Learning Model, controller, view basic process _php Example

Source: Internet
Author: User
Tags compact

Add route

Copy Code code as follows:

Route::get (' artiles ', ' articlescontroller@index ');

Creating a Controller

Copy Code code 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 results in the browser, cool!

Modify controller, return view

 The Public Function index () {
    $articles = Article::all ();

    Return view (' Articles.index ', compact (' articles '));
  }

Create a View

@extends (' layout ')

@section (' content ')
   
 

Browse results, cool!!!!

Show individual articles

To add a route that displays detailed information

Copy Code code as follows:

Route::get (' Articles/{id} ', ' articlescontroller@show ');

where {ID} is a parameter that represents the ID of the article to display, 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 ')
   
 

Trying to access in the browser:/ARTICLES/1/ARTICLES/2

Modify the Index view

@extends (' layout ')

@section (' content ')
   
 

The above mentioned is the whole content of this article, hope to be able to learn Laravel5 frame to be helpful to everybody.

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.