sywesterkowal / plugin.cakephp-slug
CakePHP 插件 - URL 转换
dev-master
2016-09-11 21:44 UTC
Requires
- php: >=5.4.16
- cakephp/cakephp: ~3.0
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-21 13:49:11 UTC
README
插件 CakePHP 用于生成友好的 URL。基于数据库,您可以生成任何数据模型的简单 URL
产品链接: http://domain.com/slugged-product-name
分类链接: http://domain.com/slugged-category-name
安装
您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。
安装 composer 包的推荐方法是
composer require sylwesterkowal/plugin.cakephp-slug
安装表
cake Migrations migrate -p Slug
配置
配置插件是为了添加 Slugg 表与需要使用生成的 URL 段的表之间的关系。
模型表
例如,在插件 "Product" 中,对于 "ProductsTable.php" 表,添加以下内容:
public function initialize(array $config)
{
...
...
$this->hasOne('Slug.Slugs', [
'className' => 'Slugs',
'foreignKey' => 'pass',
'conditions' => [
'plugin' => 'Product', // Set plugin name
'controller' => 'Products', // Set controller name
'action' => 'view' // Set action name
]
]);
$this->addBehavior('Slug.Sluggable', [
'field' => 'name', // enter the field, which will be generated slug
'plugin' => 'Product', // set plugin name
'controller' => 'Products', // set controller name
'action' => 'view', // set action name
'pass' => 'id', // set the field, which will by ID Key
]);
}
路由配置
$routes->connect(
'/:slug',
[],
['routeClass' => 'Slug.SlugRoute']
);
控制器
public $helpers = ['Slug.Slug'];
视图
在视图中使用 SlugHelper。当某些原因导致 Slug 插件离线时,参数是必需的,用于标准的 Html->link URL。
echo $this->Slug->link(
$productEntity, // Model Entity
$productEntity->name, // Anchor text
[ // parameters
'controller'=>'Products',
'action'=>'view', $productEntity->id,
'plugin'=>'Product'
]
);