【问题标题】:Pagination on Laravel website redirecting to homepageLaravel 网站上的分页重定向到主页
【发布时间】:2018-06-01 15:01:26
【问题描述】:

当我点击转到第 2 页时,我会被定向到我网站的主页。不知道为什么。

这是控制器文件中的分页代码:

public function paginate($items, $perPage = 15, $page = null, $options = [])
{
    $page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
    $items = $items instanceof Collection ? $items : Collection::make($items);
    return new LengthAwarePaginator(
        $items->forPage($page, $perPage), 
        $items->count(), 
        $perPage, 
        $page, 
        $options
    );
}

在视图文件中:

@if(!isset($_REQUEST['fees'])) {{
    $result->appends([
        'FeesRange' => request('FeesRange'),
        'sortby' => request('sortby'),
        'location' => request('location'),
        'searchItem' => request('searchItem'),
        'searchLocation' => request('searchLocation'),
        'criteria' => request('criteria'),
        'search' => request('search')
    ])->links()
}}
@endif

【问题讨论】:

  • 在你的 web.php 中为那个页面尝试 Route::any
  • 你能把分页器生成的HTML也贴出来吗?将帮助确定分页器是否生成了不正确的 URL,或者您的 laravel 应用程序是否正在访问正确的页面,但控制器/中间件正在重定向到其他地方。
  • @GoatHater - 不太清楚你所说的“那个页面”是什么意思。我有一个控制列表的刀片文件。但是列表并没有真正的页面。您只需在主页上的表格中输入您想要搜索的内容,然后您就会被定向到结果页面。不过会仔细检查。
  • @PhilCross - 这是分页栏上一个按钮的 html:<li><a href="/?searchItem=&searchLocation=&criteria=mastersdegree&search=Search+Courses&page=2">2</a></li>

标签: php laravel laravel-5 pagination


【解决方案1】:
$options = [
    'path' => Paginator::resolveCurrentPath()
]

paginate 函数的选项参数中传递它。

例子:

public function paginate($items, $perPage = 15, $page = null)
{
    $page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
    $items = $items instanceof Collection ? $items : Collection::make($items);
    return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, [
        'path' => Paginator::resolveCurrentPath()
    ]);
}

【讨论】:

  • “选项参数”是什么意思?你的意思是放在appends下面?
  • @Thomas 更新了我的答案
  • 成功了!谢谢。
猜你喜欢
  • 1970-01-01
  • 2014-10-21
  • 1970-01-01
  • 2013-09-14
  • 1970-01-01
  • 2023-01-27
  • 1970-01-01
  • 2013-08-22
  • 2021-02-17
相关资源
最近更新 更多