美文网首页
8. 小例子,实现一个简单的blog之select

8. 小例子,实现一个简单的blog之select

作者: ZhouJiping | 来源:发表于2016-06-08 15:17 被阅读38次

    1. 注册路由

    Route::get('articles', 'ArticlesController@index');
    Route::get('articles/{id}', 'ArticlesController@show');
    

    2. 创建ArticlesController类

      php artisan make:controller ArticlesController
    
    App\Http\Controllers\ArticlesController.php
    <?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    use App\Http\Requests;
    use App\Http\Controllers\Controller;
    use App\Article;
    
    class ArticlesController extends Controller
    {
        /**
         * Display a listing of the resource.
         *
         * @return \Illuminate\Http\Response
         */
        public function index()
        {
            $articles = Article::all();
            return view('articles.index', compact('articles'));
        }
    
        /**
         * Show the form for creating a new resource.
         *
         * @return \Illuminate\Http\Response
         */
        public function create()
        {
            //
        }
    
        /**
         * Store a newly created resource in storage.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return \Illuminate\Http\Response
         */
        public function store(Request $request)
        {
            //
        }
    
        /**
         * Display the specified resource.
         *
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        public function show($id)
        {
            $article = Article::findOrFail($id);
            return view('articles.show', compact('article'));
        }
    
        /**
         * Show the form for editing the specified resource.
         *
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        public function edit($id)
        {
            //
        }
    
        /**
         * Update the specified resource in storage.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        public function update(Request $request, $id)
        {
            //
        }
    
        /**
         * Remove the specified resource from storage.
         *
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        public function destroy($id)
        {
            //
        }
    }
    
    

    3. 视图层view

    resources/views

    resources/views/articles/index.blade.php

    @extends('app')
    
    @section('content')
        <h1>Articles</h1>
        <hr>
        @foreach($articles as $article)
            <h3><a href="{{ url('articles', $article->id) }}">{{ $article->title }}</a></h3>
            <div class="body">
                {{ $article->body }}
            </div>
        @endforeach
    @stop
    

    resources/views/articles/show.blade.php

    @extends('app')
    
    @section('content')
        <h1>{{ $article->title }}</h1>
        <hr>
        <div class="body">
            {{ $article->body }}
        </div>
    @stop
    

    相关文章

      网友评论

          本文标题:8. 小例子,实现一个简单的blog之select

          本文链接:https://www.haomeiwen.com/subject/ylsjdttx.html