【问题标题】:Codeigniter 4 Pagination 404 File Not Found errorCodeigniter 4分页404文件未找到错误
【发布时间】:2020-06-05 10:24:49
【问题描述】:

自从向模型函数添加分页后,我真的很难克服 CI4 中的 404 File Not Found 错误。我假设它仅与路由有关,因为它也在我的 subbrand_name 页面上抛出了相同的错误,该页面在其模型代码中不包含任何分页,并且在我将分页添加到 brand_name 页面之前工作正常.

我的路线文件:

$routes->get('shop/brands/(:segment)', 'Shop::brand_name/$1');
$routes->get('shop/brands/(:segment)/(:segment)', 'Shop::subbrand_name/$1/$2');

我的控制器:

    public function brand_name($brand_name_slug)
    {
        $model = new ShopModel();

        $data = [
            'category_menu' => $model->category_menu(),
            'brand_menu' => $model->brand_menu(),
            'nav' => $model->nav(),
            'subnav' => $model->subnav(),
            'shop' => $model->brand_name($brand_name_slug),
            'pager' => $model->pager
        ];

        if (empty($data['shop']))
        {
            throw new \CodeIgniter\Exceptions\PageNotFoundException('Cannot find the news item: '. $slug);
        }

        echo view('templates/header', $data);
        echo view('shop/view', $data);
        echo view('templates/footer', $data);
    }

我的模特:

    public function brand_name($brand_name_slug)
    {
        return $this
                    ->table('shop a')
                    ->select()
                    ->join('(SELECT sku, MIN(sale_price) AS sale_price FROM shop GROUP BY sku) AS b', 'a.sku = b.sku AND a.sale_price = b.sale_price')
                    ->where('availability', 'in stock')
                    ->where('a.sku !=', '')
                    ->where('brand_name_slug', $brand_name_slug)
                    ->groupBy('a.sku')
                    ->orderBy('brand_name, subbrand_name, product, size, unit')
                    ->paginate(15);
    }

【问题讨论】:

  • 您能否在获取 404 时添加您尝试访问的 url?
  • www.seixwebdev.co.uk/shop/brands/hugo-boss
  • 另外,根本没有编辑 .htacess 文件。

标签: codeigniter pagination routes codeigniter-4


【解决方案1】:

事实证明,这与路由无关......只是简单地说,我的 JOIN 有问题。

通过将参数的 JOIN 部分中的 shop aa.sku 替换为 shopshop.sku 来对其进行排序。完美运行...终于!

    public function brand_name($brand_name_slug)
    {
        return $this
                    ->table('shop')
                    ->select('*')
                    ->join('(SELECT sku, MIN(sale_price) AS sale_price FROM shop GROUP BY sku) AS b', 'shop.sku = b.sku AND shop.sale_price = b.sale_price')
                    ->where('availability', 'in stock')
                    ->where('shop.sku !=', '')
                    ->where('brand_name_slug', $brand_name_slug)
                    ->groupBy('shop.sku')
                    ->orderBy('brand_name, subbrand_name, product, size, unit')
                    ->paginate(15);
    }

【讨论】:

    猜你喜欢
    • 2022-08-21
    • 2015-03-30
    • 1970-01-01
    • 2015-09-19
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    • 2016-12-06
    • 1970-01-01
    相关资源
    最近更新 更多