1. 注册路由
Route::get('articles', 'ArticlesController@index');
Route::get('articles/{id}', 'ArticlesController@show');
2. 创建ArticlesController类
php artisan make:controller ArticlesController
<?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/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