Leafiny Documentation
Rewrite
Leafiny offers a rewrite URL system that can be used with all routes.
Note: If you don't need to rewrite the routes, simply remove the Leafiny_Rewrite module.
Routes
Routes are declared in config files (see Routes section). For example:
/** modules/Frontend/etc/config.php **/
'page' => [
'/foo/example.html' => [
/* ... */
],
],
/* ... */
If we want to rewrite /foo/example.html with /bar/example.html, we add a new rewrite.
From the admin
Menu: URL > Rewrites
Click on the Add button.
Programmatically
/** @var Rewrite_Model_Rewrite $rewrite */
$rewrite = App::getObject('model', 'rewrite');
if (!$rewrite->exists('/foo/example.html')) {
$object = new Leafiny_Object();
$object->setData(
[
'source_identifier' => '/foo/example.html',
'target_identifier' => '/bar/example.html',
]
);
$rewrite->save($object);
}
Entities
The rewrite module allows to automatically add rewrite for entities. For example, the route for a simple CMS page is declared as follows:
/** modules/Leafiny_Cms/etc/config.php **/
'page' => [
'/page/*.html' => [
/* ... */
],
],
/* ... */
We don't want to have /page/*.html, but only *.html.
For the rewrite model, we have an entity rewrite configuration:
/** modules/Leafiny_Cms/etc/config.php **/
'model' => [
/* ... */
'rewrite' => [
'entity' => [
'cms_page' => [ // The model identifier
'enabled' => 1, // Enabled or disabled the rewrite
'table' => 'cms_page', // The entity table name
'column' => 'path_key', // The entity URL path column
'source' => '/*.html', // The URL we want
'target' => '/page/*.html', // The original route
]
]
],
],
/* ... */
On the CMS page save, the rewrite is added. When /my-page.html is opened, the system use /page/my-page.html.
Note: Be sure to have an unique URL. If 2 entities use the same rewrite, one of them will be never retrieved.
Get the URL
In any template, we use the getUrlRewrite method to get the URL:
{{ page.getUrlRewrite('my-page', 'cms_page') }}
Make sure that the option is enabled for the rewrite model. Otherwise, the original route will be returned.
/** etc/config.default.php **/
'model' => [
'rewrite' => [
'enabled' => 1,
],
/* ... */
],
Refresh
From the admin
When an entity URL path is updated, the old rewrite is never removed. You can remove it manually from the admin: URL > Rewrites.
For a total refresh, remove all the entity URLs by filtering on the type. Then click the Refresh button on the rewrites page.
Programmatically
/** @var Rewrite_Model_Rewrite $rewriteModel */
$rewriteModel = App::getObject('model', 'rewrite');
$objectType = 'category'; // category, catalog_product, cms_page, blog_post
$filters = [
[
'column' => 'object_type',
'value' => $objectType,
]
];
$rewrites = $rewriteModel->getList($filters);
foreach ($rewrites as $rewrite) {
$rewriteModel->delete((int)$rewrite->getData('rewrite_id'));
}
$rewriteModel->refreshAll($objectType);