Home > Article > Backend Development > Laravel 5 Basics (8) - Basic Process of Model, Controller, and View
Route::get('artiles', 'ArticlesController@index');
php artisan make:controller ArticlesController --plain
You can see the returned JSON results 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
Browse the results, COOL! ! ! !
Add route showing details
Route::get('articles/{id}', 'ArticlesController@show');
Among them, {id}
is a parameter, indicating the id of the article to be displayed. Modify the controller:
public function show($id) {
$article = Article::find($id);
//若果找不到文章
if (is_null($article))
{
//生产环境 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.
New View
@extends('layout')
@section('content')
{{$article->title}}
{{$article->body}}
@stop
Try to access in your browser: /articles/1 /articles/2
Modify index view
@extends('layout')
@section('content')
Articles
@foreach($articles as $article)
{{--这种方式可以--}}
{{$article->title}}
{{--这种方式更加灵活,不限制路径--}}
{{$article->title}}
{{--还可以使用--}}
{{$article->title}}
{{$article->body}}
@endforeach
@stop
The above introduces the basics of Laravel 5 (8) - the basic process of model, controller and view, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.