paginator->links() 导致 Method Illuminate\View\View::__toString() 不能抛出异常
paginator->links() results in Method Illuminate\View\View::__toString() must not throw an exception
我正在用 Laravel 4.2、bootstrap 和 MySQL 制作一个项目。
现在我遇到了以下问题。
我检索数据库记录,然后使用分页显示它们。我在几个页面上以相同的方式执行此操作,因此我创建了一个 blade 模板以不再重复。当我使用 Paginator
的 links
方法时,问题就来了。使用 null 参数时,该方法不会抛出任何异常,但它无法正常工作,因为无论您身在何处,链接总是会将您重定向到主页。
在 Laravel 4.2 文档中是这样说的:
If you would like to specify a custom view to use for pagination, you
may pass a view to the links method:
<?php echo $users->links('view.name'); ?>
但是如果我尝试在我的 blade 模板中做这样的事情:
{{ $products->links('client.pages.search_results') }}
我得到以下异常:
方法Illuminate\View\View::__toString()不能抛出异常
编辑
这是 search_results
模板:
@extends('client.layouts.client')
@section('content')
@include('client.includes.products', ['header_title' => "Results for $search",
'show_platform_icon' => true,
'view_name' => 'client.pages.search_results'])
@stop
这是 products
模板:
<div>
<div class="navbar navbar-default">
<div class="navbar-collapse navbar-header">
<h3>{{ $header_title }}</h3>
</div>
<div class="navbar-collapse navbar-right">
{{ Form::open(['class' => 'navbar-form']) }}
<div class="form-group">
<select class="form-control" id="sorter" >
<option disabled selected>Order by</option>
<option value="{{ URL::route('index') }}/name/asc">Name asc</option>
<option value="{{ URL::route('index') }}/name/desc">Name desc</option>
@if(Auth::check())
<option value="{{ URL::route('index') }}/discount/asc">Discount asc</option>
<option value="{{ URL::route('index') }}/discount/desc">Discount desc</option>
@else
<option value="{{ URL::route('index') }}/price/asc">Price asc</option>
<option value="{{ URL::route('index') }}/price/desc">Price desc</option>
@endif
</select>
</div>
{{ Form::close() }}
</div>
</div>
<div class="col-md-12">
<ul class="thumnails col-md-11">
@foreach ($products as $index => $product)
<li class="col-md-3 col-md-offset-1 thumbnail">
<a href="#product">
{{ HTML::image($product->game->thumbnail_image_path, $product->game->name) }}
</a>
<div class="caption">
@if($show_platform_icon)
<a href="#platform" class="pull-left">
{{ HTML::image($product->platform->icon_path, $product->platform->name) }}
</a>
@endif
<a href="#product">
<div class="text-center">
@if(Auth::check() && $product->discount)
<h3>
{{ floatval($product->price * ((100 - $product->discount) / 100)) }} €
<span class="label label-default">-{{ floatval($product->discount)}}%</span>
</h3>
@else
<h3>{{ floatval($product->price) }} €</h3>
@endif
</div>
</a>
</div>
</li>
@endforeach
</ul>
<div class="col-md-4 col-md-offset-4 text-center">
{{ $products->links($view_name) }}
</div>
</div>
</div>
来自问题下方的评论
我认为您误解了 links('view')
的工作原理。您传递的视图将用于显示分页链接。为了给你一个想法,这是默认的:slider-3.php
Ok, i see my error. Now i have to figure out why when i use links() it redirects me to the main page instead of staying in the actual page. Thanks a lot.
渲染页面中的链接是什么样子的?应该是 ?page=1
几个小时后...
I fixed the issue. I was screwing up the pagination process with one of my routes. The paginator was trying to access the page segment but the route was redirecting that request to the main page. I couldn't do it without your help.
我正在用 Laravel 4.2、bootstrap 和 MySQL 制作一个项目。 现在我遇到了以下问题。
我检索数据库记录,然后使用分页显示它们。我在几个页面上以相同的方式执行此操作,因此我创建了一个 blade 模板以不再重复。当我使用 Paginator
的 links
方法时,问题就来了。使用 null 参数时,该方法不会抛出任何异常,但它无法正常工作,因为无论您身在何处,链接总是会将您重定向到主页。
在 Laravel 4.2 文档中是这样说的:
If you would like to specify a custom view to use for pagination, you may pass a view to the links method:
<?php echo $users->links('view.name'); ?>
但是如果我尝试在我的 blade 模板中做这样的事情:
{{ $products->links('client.pages.search_results') }}
我得到以下异常:
方法Illuminate\View\View::__toString()不能抛出异常
编辑
这是 search_results
模板:
@extends('client.layouts.client')
@section('content')
@include('client.includes.products', ['header_title' => "Results for $search",
'show_platform_icon' => true,
'view_name' => 'client.pages.search_results'])
@stop
这是 products
模板:
<div>
<div class="navbar navbar-default">
<div class="navbar-collapse navbar-header">
<h3>{{ $header_title }}</h3>
</div>
<div class="navbar-collapse navbar-right">
{{ Form::open(['class' => 'navbar-form']) }}
<div class="form-group">
<select class="form-control" id="sorter" >
<option disabled selected>Order by</option>
<option value="{{ URL::route('index') }}/name/asc">Name asc</option>
<option value="{{ URL::route('index') }}/name/desc">Name desc</option>
@if(Auth::check())
<option value="{{ URL::route('index') }}/discount/asc">Discount asc</option>
<option value="{{ URL::route('index') }}/discount/desc">Discount desc</option>
@else
<option value="{{ URL::route('index') }}/price/asc">Price asc</option>
<option value="{{ URL::route('index') }}/price/desc">Price desc</option>
@endif
</select>
</div>
{{ Form::close() }}
</div>
</div>
<div class="col-md-12">
<ul class="thumnails col-md-11">
@foreach ($products as $index => $product)
<li class="col-md-3 col-md-offset-1 thumbnail">
<a href="#product">
{{ HTML::image($product->game->thumbnail_image_path, $product->game->name) }}
</a>
<div class="caption">
@if($show_platform_icon)
<a href="#platform" class="pull-left">
{{ HTML::image($product->platform->icon_path, $product->platform->name) }}
</a>
@endif
<a href="#product">
<div class="text-center">
@if(Auth::check() && $product->discount)
<h3>
{{ floatval($product->price * ((100 - $product->discount) / 100)) }} €
<span class="label label-default">-{{ floatval($product->discount)}}%</span>
</h3>
@else
<h3>{{ floatval($product->price) }} €</h3>
@endif
</div>
</a>
</div>
</li>
@endforeach
</ul>
<div class="col-md-4 col-md-offset-4 text-center">
{{ $products->links($view_name) }}
</div>
</div>
</div>
来自问题下方的评论
我认为您误解了 links('view')
的工作原理。您传递的视图将用于显示分页链接。为了给你一个想法,这是默认的:slider-3.php
Ok, i see my error. Now i have to figure out why when i use links() it redirects me to the main page instead of staying in the actual page. Thanks a lot.
渲染页面中的链接是什么样子的?应该是 ?page=1
几个小时后...
I fixed the issue. I was screwing up the pagination process with one of my routes. The paginator was trying to access the page segment but the route was redirecting that request to the main page. I couldn't do it without your help.