Home > Backend Development > PHP Tutorial > Laravel 五 基础(八)- 模型、控制器、视图基础流程

Laravel 五 基础(八)- 模型、控制器、视图基础流程

WBOY
Release: 2016-06-13 12:17:19
Original
841 people have browsed it

Laravel 5 基础(八)- 模型、控制器、视图基础流程

  • 添加路由
<code>Route::get(&#39;artiles&#39;, &#39;[email&#160;protected]&#39;);</code>
Copy after login
  • 创建控制器
<code> php artisan make:controller ArticlesController --plain</code>
Copy after login
  • 修改控制器
<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

可以在浏览器中看到返回的 JSON 结果,cool!

修改控制器,返回视图

<code>	public function index() {        $articles = Article::all();        return view(&#39;articles.index&#39;, compact(&#39;articles&#39;));    }</code>
Copy after login

创建视图

<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

浏览结果,COOL!!!!

  • 显示单个文章

添加显示详细信息的路由

<code>Route::get(&#39;articles/{id}&#39;, &#39;[email&#160;protected]&#39;);</code>
Copy after login

其中,{id} 是参数,表示要显示的文章的 id,修改控制器:

<code>    public function show($id) {        $article = Article::find($id);        //若果找不到文章        if (is_null($article))        {            //生产环境 APP_DEBUG=false            abort(404);        }        return view(&#39;articles.show&#39;, compact(&#39;article&#39;));    }</code>
Copy after login

laravel 提供了更加方便的功能,修改控制器:

<code>    public function show($id) {        $article = Article::findOrFail($id);        return view(&#39;articles.show&#39;, compact(&#39;article&#39;));    }</code>
Copy after login

It's cool.

新建视图

<code>@extends(&#39;layout&#39;)@section(&#39;content&#39;)    <h1>{{$article->title}}</h1>    <article>        {{$article->body}}    </article>@stop</code>
Copy after login

在浏览器中尝试访问:/articles/1 /articles/2

修改index视图

<code>@extends(&#39;layout&#39;)@section(&#39;content&#39;)    <h1>Articles</h1>    <hr/>    @foreach($articles as $article)        <article>            <h2>                {{--这种方式可以--}}                <a href="/articles/{{$article->id}}">{{$article->title}}</a>                {{--这种方式更加灵活,不限制路径--}}<br>                <a href="{{action(&#39;[email&#160;protected]&#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
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