自定义路由类的Cakephp v3致命错误

时间:2019-05-14 22:06:56

标签: php cakephp url-routing cakephp-3.0

我正在尝试使用自定义路由类。

有时候我重定向到

这样的网址
www.site.com/neumaticos-bridgestone 

它返回致命错误。

但是当我刷新页面时,错误被隐藏了

我尝试实现许多自定义路由类

routes.php

$routes->connect('/neumaticos-:marca', ['controller' => 'Pages', 'action' => 'brand'],
     ['routeClass' => 'BrandRoute'])
        ->setPass(['marca']);    
$routes->connect('/neumaticos-para-:slug', ['controller' => 'Pages', 'action' => 'vehicleVersion'],
     ['routeClass' => 'VehicleVersionRoute'])
        ->setPass(['slug']); 

BrandRoute.php

namespace Cake\Routing\Route;

use Cake\Utility\Inflector;
use Cake\ORM\TableRegistry;
use Cake\ORM\Query;


class BrandRoute extends Route
{

    public function parse($url, $method = '')
    {
        $params = parent::parse($url, $method);
        if (!$params) {
            return false;
        }
        //return false;
        $brands = TableRegistry::get('ProductBrands');

        $slug = strtolower($params['marca']);
        $brand = $brands->find()
            ->where([
                'ProductBrands.slug' => $slug
            ])
            ->count();
        if($brand > 0){
            return $params;
        }else{
            return false;
        }
        return false;
    }
}

致命错误是下一个:

Error: Cake\Routing\RouteCollection::parseRequest(): The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "Cake\Routing\Route\BrandRoute" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition 
File /var/www/www.site.com/public_html/vendor/cakephp/cakephp/src/Routing/RouteCollection.php 
Line: 205

1 个答案:

答案 0 :(得分:0)

  

$ routes-> connect('/:slug',['controller'=>'Categories','action'=>   'view'],['pass'=> ['slug']]));

链接将为

  

$ this-> HTML-> link($ category-> title,['controller'=>'categories',   'action'=>'view',$ category-> slug])?>

使用控制器方法将条形文本分为类别和ID等