我目前可以显示索引页面以及帖子列表。用户可以选择帖子以查看帖子详细信息。为此,我有:
路线:
Route::get('/', 'PostsController@showPosts');
Route::get('post/{slug}', 'PostsController@showPostDetails');
控制器:
<?php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
class PostsController extends Controller
{
public function showPosts()
{
$posts = Post::simplePaginate(2);
return view('index', ['posts' => $posts]);
}
public function showPostDetails($slug)
{
$post = Post::findBySlug($slug);
return view('post.show',['post'=>$post]);
}
}
型号:
public static function findBySlug($slug)
{
return static::where('slug', $slug)->first();
}
索引
@extends('layouts.index')
@section('header')
<!-- Page Header -->
<header class="masthead" style="background-image: url('img/home-bg.jpg')">
<div class="overlay"></div>
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<div class="site-heading">
<h1>Train Testing</h1>
<span class="subheading">Testing Times</span>
</div>
</div>
</div>
</div>
</header>
@stop
@section('content')
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
@foreach ($posts as $post)
@include('partials.post', ['post' => $post])
@endforeach
{{ $posts->links() }}
</div>
</div>
@stop
分音/ post.blade.php
<div class="post-preview">
<a href="/post/{{ $post->slug }}">
<h2 class="post-title">
{{ $post->title }}
</h2>
<h3 class="post-subtitle">
{{ $post->excerpt }}
</h3>
</a>
<p class="post-meta">Posted by
<a href="#">{{ $post->author->name }}</a>
on {{ $post->created_at->format('l d F, Y') }}</p>
</div>
<hr>
我现在想在所有其他页面上显示帖子。我的其他页面目前有路由Route::get('{slug}', 'PagesController@show');
并且最初要回溯到现有代码是有意义的,所以像我在主页上那样使用Route::get('{slug}', 'PostsController@showPosts');
来显示那里的帖子。
但是我不确定解决这个问题的最佳方法,因为我相信你不能为一条路线安装两个控制器。
对于我目前拥有的其他页面:
控制器:
<?php
namespace App\Http\Controllers;
use App\Page;
use Illuminate\Http\Request;
class PagesController extends Controller
{
public function show($slug)
{
$page = Page::findBySlug($slug);
return view('page', ['page' => $page]);
}
}
page.blade:
@extends('layouts.index')
@section('header')
<!-- Page Header -->
<header class="masthead" style="background-image: url('/storage/{{ $page->image }}')">
<div class="overlay"></div>
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<div class="page-heading"> <h1>{!! $page->title !!}</h1>
<span class="subheading"></span>
</div>
</div>
</div>
</div>
</header>
@stop
@section('content')
<!-- Main Content -->
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
{!! $page->body !!}
</div>
</div>
</div>
@stop
答案 0 :(得分:0)
页面控制器:
use App\Page;
use App\Post;
use Illuminate\Http\Request;
class PagesController extends Controller
{
public function show($slug)
{
$posts = Post::simplePaginate(2);
$page = Page::findBySlug($slug);
return view('page', ['page' => $page, 'posts' => $posts]);
}
然后在page.blade.php中我可以调用$ posts而不会出现任何错误:
@foreach ($posts as $post)
@include('partials.post', ['post' => $post])
@endforeach
{{ $posts->links() }}
不确定这是否是最好的方式,但它有效。