我是laravel的新手。我的应用程序有一个前端和一个后端。我已经设置了路线,控制器和其他必要的东西。但是每当我点击前端的帖子标题时,它就会重定向到后端而没有显示任何内容。我不知道我的问题在哪里。
[它重定向到后端][2]
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Auth::routes();
Route::get('/home', 'Backend\HomeController@index');
Route::get('/', [
'uses' =>'BlogController@index',
'as'=>'blog'
]);
Route::get('blog/{post}', [
'uses' => 'BlogController@show',
'as' => 'blog.show'
]);
Route::get('tag/{tag}', [
'uses' => 'BlogController@tag',
'as' => 'tag'
]);
Route::get('category/{category}', [
'uses' =>'BlogController@category',
'as'=>'category'
]);
Route::get('author/{author}', [
'uses' =>'BlogController@author',
'as'=>'author'
]);
Route::resource('backend/blog/', 'Backend\BlogController');
Route::resource('backend/categories','Backend\CategoriesController');
Route::resource('backend/users','Backend\UsersController');
Route::put('/backend/blog/restore/{blog}',[
'uses' =>'Backend\BlogController@restore',
'as'=>'backend.blog.restore'
]);
Route::delete('/backend/blog/force-destroy/{blog}',[
'uses' =>'Backend\BlogController@forceDestroy',
'as'=>'backend.blog.force-destroy'
]);
Route::get('/backend/users/confirm/{users}', [
'uses' => 'Backend\UsersController@confirm',
'as' => 'backend.users.confirm'
]);
这是控制器
<?php
namespace App\Http\Controllers;
use App\Category;
use App\Post;
use App\Tag;
use App\User;
use Illuminate\Http\Request;
class BlogController extends Controller
{
//
protected $limit =5;
public function index(){
$posts = Post::with('author')
->latestFirst()
->published()
->filter(request('term'))
->simplePaginate($this->limit);
return view("blogs.index", compact('posts'));
}
public function category(Category $category){
$categoryName = $category->title;
$posts = $category->posts()
->with('author')
->latestFirst()
->published()
->simplePaginate($this->limit);
return view ("blogs.index",compact('posts','categoryName'));
}
public function show(Post $post)
{
$post->increment('view_count');
return view("blogs.show",compact('post'));
}
public function author(User $author)
{
$authorName = $author->name;
$posts = $author->posts()
->with('category')
->latestFirst()
->published()
->simplePaginate($this->limit);
return view("blogs.index", compact('posts', 'authorName'));
}
public function tag(Tag $tag)
{
$tagName = $tag->title;
$posts = $tag->posts()
->with('author', 'category')
->latestFirst()
->published()
->simplePaginate($this->limit);
return view("blogs.index", compact('posts', 'tagName'));
}
}
这是我的前端index.blade.php文件
@extends('layouts.main')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8">
@if (! $posts->count())
<div class="alert alert-warning">
<p>Nothing Found</p>
</div>
@else
@include('blogs.alert')
@foreach($posts as $post)
<article class="post-item">
@if ($post->image)
<div class="post-item-image">
<a href="{{ route('blog.show', $post->slug) }}">
<img src="{{ $post->image?asset('/images/img/' . $post->image):'http://placehold.it/400x400' }}" alt="">
</a>
</div>
@endif
<div class="post-item-body">
<div class="padding-10">
<h2><a href="{{ route('blog.show', $post->slug) }}">{{ $post->title }}</a></h2>
{!! $post->excerpt_html !!}
</div>
<div class="post-meta padding-10 clearfix">
<div class="pull-left">
<ul class="post-meta-group">
<li><i class="fa fa-user"></i><a href="{{ route('author', $post->author->slug) }}"> {{ $post->author->name }}</a></li>
<li><i class="fa fa-clock-o"></i><time> {{ $post->date }}</time></li>
<li><i class="fa fa-folder"></i><a href="{{ route('category', $post->category->slug) }}"> {{ $post->category->title }}</a></li>
<li><i class="fa fa-tag"></i>{!! $post->tags_html !!}</li>
<li><i class="fa fa-comments"></i><a href="#">4 Comments</a></li>
</ul>
</div>
<div class="pull-right">
<a href="{{ route('blog.show', $post->slug) }}">Continue Reading »</a>
</div>
</div>
</div>
</article>
@endforeach
@endif
<nav>
{{ $posts->appends(request()->only(['term']))->links() }}
</nav>
</div>
@include('layouts.sidebar')
</div>
</div>
@endsection
这是我的show.blade.php文件
@extends('layouts.main')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8">
<article class="post-item post-detail">
{{--@if (($post->image))--}}
<div class="post-item-image">
<img src="{{ $post->image?asset('/images/img/' . $post->image):'http://placehold.it/400x400' }}" alt="">
</div>
{{--@endif--}}
<div class="post-item-body">
<div class="padding-10">
<h1>{{$post->title}}</h1>
<div class="post-meta no-border">
<ul class="post-meta-group">
<li><i class="fa fa-user"></i><a href="{{route('author',$post->author->slug)}}"> {{$post->author->name}}</a></li>
<li><i class="fa fa-clock-o"></i><time>{{$post->date}}</time></li>
<li><i class="fa fa-folder"></i><a href="{{route('category',$post->category->slug)}}"> {{$post->category->title}}</a></li>
<li><i class="fa fa-tag"></i>{!! $post->tags_html !!}</li>
<li><i class="fa fa-comments"></i><a href="#">4 Comments</a></li>
</ul>
</div>
{!! $post->body_html!!}
</div>
</div>
</article>
<article class="post-author padding-10">
<div class="media">
<div class="media-left">
<?php $author = $post->author?>
<a href="{{route('author', $author->slug)}}">
<img alt="{{$author->name}}" width="100" height="100" src="{{$author->gravatar()}}" class="media-object">
</a>
</div>
<div class="media-body">
<h4 class="media-heading"><a href="{{route('author',$author->slug)}}">{{$author->name}}</a></h4>
<div class="post-author-count">
<a href="{{route('author',$author->slug)}}">
<i class="fa fa-clone"></i>
<?php $postCount = $author->posts()->published()->count()?>
{{$postCount}} {{str_plural('post', $postCount)}}
</a>
</div>
{!! $author->bio_html !!}
</div>
</div>
</article>
{{--Comments here--}}
</div>
@include('layouts.sidebar')
</div>
</div>
@stop
这是我的RouteServiceProvider.php代码
<?php
namespace App\Providers;
use App\Post;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Route;
//use Symfony\Component\Routing\Annotation\Route;
//use Symfony\Component\Routing\Route;
//use Symfony\Component\Routing\Router;
class RouteServiceProvider extends ServiceProvider
{
/**
* This namespace is applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* @var string
*/
protected $namespace = 'App\Http\Controllers';
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
//
parent::boot();
Route::bind('post',function ($slug){
return Post::published()->where('slug', $slug)
->first();
});
}
/**
* Define the routes for the application.
*
* @return void
*/
public function map()
{
$this->mapApiRoutes();
$this->mapWebRoutes();
//
}
/**
* Define the "web" routes for the application.
*
* These routes all receive session state, CSRF protection, etc.
*
* @return void
*/
protected function mapWebRoutes()
{
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
}
/**
* Define the "api" routes for the application.
*
* These routes are typically stateless.
*
* @return void
*/
protected function mapApiRoutes()
{
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
}
答案 0 :(得分:-1)
public function show(Post $post) <-- this need full object for getting result
{
$post->increment('view_count');
return view("blogs.show",compact('post'));
}
just try this
public function show($slug){
$post = Post::where('slug', $slug)->firstorfail();
$post->increment('view_count');
return view("blogs.show",compact('post'));
}