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

WBOY
Release: 2016-08-08 09:26:47
Original
863 people have browsed it
  • Add route
<code>Route::get('artiles', 'ArticlesController@index');</code>
Copy after login
  • Create Controller
<code> php artisan make:controller ArticlesController --plain</code>
Copy after login
  • Modify controller
<code><?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;
    }

}</code>
Copy after login

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

Modify the controller and return to the view

<code>	public function index() {
        $articles = Article::all();

        return view(&#39;articles.index&#39;, compact(&#39;articles&#39;));
    }</code>
Copy after login

Create View

<code>@extends(&#39;layout&#39;)

@section(&#39;content&#39;)
    <h1>Articles</h1>

    @foreach($articles as $article)
        <article>
            <h2>{{$article->title}}</h2>

            <div class="body">{{$article->body}}</div>
        </article>
    @endforeach
@stop</code>
Copy after login

Browse the results, COOL! ! ! !

  • Show single post

Add route showing details

<code>Route::get('articles/{id}', 'ArticlesController@show');</code>
Copy after login

Among them, {id} is a parameter, indicating the id of the article to be displayed. Modify the controller:

<code>    public function show($id) {
        $article = Article::find($id);

        //若果找不到文章
        if (is_null($article))
        {
            //生产环境 APP_DEBUG=false
            abort(404);
        }
        return view('articles.show', compact('article'));
    }</code>
Copy after login

Laravel provides more convenient functions to modify the controller:

<code>    public function show($id) {
        $article = Article::findOrFail($id);

        return view('articles.show', compact('article'));
    }</code>
Copy after login

It's cool.

New View

<code>@extends('layout')

@section('content')
    <h1>{{$article->title}}</h1>

    <article>
        {{$article->body}}
    </article>
@stop</code>
Copy after login

Try to access in your browser: /articles/1 /articles/2

Modify index view

<code>@extends('layout')

@section('content')
    <h1>Articles</h1>
    <hr/>
    @foreach($articles as $article)
        <article>
            <h2>
                {{--这种方式可以--}}
                <a href="/articles/{{$article->id}}">{{$article->title}}</a>
                {{--这种方式更加灵活,不限制路径--}}<br>
                <a href="{{action(&#39;ArticlesController@show&#39;, [$article->id])}}">{{$article->title}}</a>
                {{--还可以使用--}}<br>
                <a href="{{url(&#39;/articles&#39;, $article->id)}}">{{$article->title}}</a>
            </h2>

            <div class="body">{{$article->body}}</div>
        </article>
    @endforeach
@stop</code>
Copy after login

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.

Related labels:
gt lt
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template