Home  >  Article  >  Backend Development  >  Laravel 5 Basics (8) - Basic Process of Model, Controller, and View

Laravel 5 Basics (8) - Basic Process of Model, Controller, and View

WBOY
WBOYOriginal
2016-08-08 09:26:47749browse
  • Add route
Route::get('artiles', 'ArticlesController@index');
  • Create Controller
 php artisan make:controller ArticlesController --plain
  • Modify controller

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! ! ! !

  • Show single post

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) @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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn