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

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 文章分类 后台文章分类列表页模板导的详细步骤建立数据表blog_category,并添加相应的文章字段使用php ...
    JoyceZhao阅读 5,718评论 0 14
  • 校园失物招领平台开发 ——基于laravel框架构建最小内容管理系统 摘要 ​ 针对目前大学校园人口密度大、人群活...
    蓝莲花xzsky阅读 11,373评论 8 54
  • 已经同步到gitbook,想阅读的请转到gitbook: Django 1.10 中文文档 URL dispatc...
    leyu阅读 14,818评论 0 16
  • 很多时候,我们对阅读的切实受益很难说清道明。因为好多需要技能的行业似乎没有用上。所以大人觉得长大我不从事文职,...
    平紫衿阅读 3,590评论 1 2
  • 昨晚看了几篇文章,有触动有收获。 第一篇是关于写作的,说到写作要先阅读,不能只看网文,要读书,要系统的吸收知识。 ...
    林安君阅读 2,435评论 0 1