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.