跳转到内容

第三方插件

4SEF 使用一段专门为特定组件定制的代码来为该组件构建 SEF 网址。它支持所有 Joomla 原生内容类型,以及一些常用的第三方扩展。

对于没有专用支持插件的组件,4SEF 会改为使用该组件自身的 router.php 文件,该文件通常供 Joomla SEF 网址系统使用。这一过程是自动的,在大多数情况下都能正常工作。

如果您想为自己使用或开发的组件提供一套更精细的网址,可以为 4SEF 创建一个 SEF URL 驱动程序来实现。

驱动程序至少是一个单独的 PHP 类文件,您可以将它存放在任何位置。例如,它可以是您自己组件的一部分,也可以放在一个专用插件中(请参阅下文的加载驱动程序段落)。

驱动程序扩展 4SEF 提供的一个基类,主要有 3 个作用:

  • 根据非 SEF 变量构建 SEF URL,类似于 Joomla 自身的 router.php 文件
  • 构建“规范化”的非 SEF URL。其中大部分工作 4SEF 已经完成,但您可能希望删除某些组件特有的查询变量。
  • 可选:确定某个 SEF URL 是否应保留为非 SEF,这有时很方便,还能节省空间
<?php
/**
* Project: 4SEF
*
* @package 4SEF
* @copyright Copyright Weeblr llc - 2022
* @author Yannick Gaultier - Weeblr llc
* @license GNU General Public License version 3; see LICENSE.md
* @version 1.0.0
* @date 2022-06-03
*/
namespace Weeblr\Forsef\Platform\Extensions;
use Joomla\CMS\Uri;
use Weeblr\Forsef\Data;
use Weeblr\Forsef\Helper;
use Weeblr\Wblib\Forsef\Wb;
use Weeblr\Wblib\Forsef\System;
use Weeblr\Wblib\Forsef\Factory;
// no direct access
defined('_JEXEC') || die();
/**
* Sample plugin to build URL for a com_sample component.
*
* - Drop the "com_" at the start from the component to name the class
* - Below are only the most important methods. For more advanced logic,
* be sure to check the built-in plugins in
*
* /plugins/system/forsef/platform/extensions
*
*/
class Sample extends Base
{
/**
* @var Helper\Platform
*/
private $platformHelper;
/**
* Stores factory instance.
*
* @param string $option Extension this applies to, in com_xxx format.
* @param array $options Can inject custom factory and platform.
*/
public function __construct($option, $options = [])
{
parent::__construct($option, $options);
$this->platformHelper = $this->factory
->getA(Helper\Platform::class);
}
/**
* Builds the SEF URL for a non-sef.
*
* @param Uri\Uri $uriToBuild
* @param Uri\Uri $platformUri
* @param Uri\Uri $originalUri
*
* @return \array
* @throws \Exception
*/
public function build($uriToBuild, $platformUri, $originalUri)
{
$sefSegments = parent::build($uriToBuild, $platformUri, $originalUri);
// Read whatever variables you need to build your SEF URL.
$view = $uriToBuild->getVar('view');
$layout = $uriToBuild->getVar('layout');
$id = $uriToBuild->getVar('id');
$Itemid = $uriToBuild->getVar('Itemid');
// and add strings to the $sefSegments array to build the URL.
// $sefSegments[] = 'something'
// Add a / as the last element of the sefSegments array to prevent the
// .html suffix to be appended to the URL later on (if set in 4SEF configuration).
// $sefSegments[] = '/';
return $sefSegments;
}
/**
* Participate in building a normalized non-sef URL based on an incoming URI. Query vars values are URL-encoded.
* Stripping slugs, sorting vars and possibly other things are taken care globally. Only plugin-specific
* vars processing should happen here. For instance, stripping pagination variables if the plugin
* handles pagination dynamically.
*
* @param array $vars
* @return array
*/
public function buildNormalizedNonSef($vars)
{
return $this->nonSefHelper->stripFeedVars(
parent::buildNormalizedNonSef(
$vars
)
);
}
/**
* Check if passed URI is for an extension configured to be left non-sef.
*
* @param Uri\Uri $uriToBuild
* @return bool
*/
public function shouldLeaveNonSef($uriToBuild)
{
// read whatever variables you need to decide whether a
// URL must be left non-sef
$view = $uriToBuild->getVar('view');
$task = $uriToBuild->getVar('task');
$layout = $uriToBuild->getVar('layout');
$a_id = $uriToBuild->getVar('a_id');
// then check these variables and return true
// if this URL must be left non-sef.
// by default, all URLs are made SEF
return false;
}
}

由于您的驱动程序只是一个特定的 PHP 类,只要该 PHP 文件被包含一次,4SEF 就会在需要时自动检测并使用它。

有 3 种方式可以做到这一点,如下所述。我们建议构建一个真正的 Joomla 插件来封装您的驱动程序:对最终用户来说,这在更新等方面是最方便的方式,不过这 3 种方式都完全可行。

您只需在自己扩展中的任意位置执行 include_once 'my_4sef_driver.php',即可使驱动程序可用。如果它的命名正确(参见上面的示例),4SEF 就能检测并使用它。

就在需要构建第一个 SEF URL 之前,4SEF 会触发一个事件,让您包含您的驱动程序。您可以在扩展中添加以下代码来加载驱动程序:

<?php
// Your extension own code here
// Include use statement for 4SEF factory class
use Weeblr\Forsef\Factory as ForsefFactory;
// somewhere in your extension, add the following to respond to 4SEF event:
ForsefFactory::get()->getThe('hook')->add(
'forsef_on_load_plugins',
function () {
// include your plugin file here
// Sample code to include your driver
$filename = '/absolute/path/to/your/driver/componentname.php';
if (file_exists($filename))
{
include_once $filename;
}
}
);

您可以构建一个真正的 Joomla 插件,用来封装驱动程序并将其交付给用户。这其实相当简单,而且能确保驱动程序足够早地加载。

以下是此类插件的结构:

4SEF 网址插件的目录结构

/plugin/sample.php 文件包含您的驱动程序,如上文驱动程序示例代码段落所述。

以下是清单文件 sample.xml:

<?xml version="1.0" encoding="utf-8"?>
<extension type="plugin" group="forsef" method="upgrade">
<name>PLG_FORSEF_SAMPLE</name>
<author>Yannick Gaultier - Weeblr llc</author>
<creationDate>2022-06-03</creationDate>
<copyright>Copyright Weeblr llc - 2022</copyright>
<license>GNU General Public License version 3; see LICENSE.md</license>
<authorEmail>[email protected]</authorEmail>
<authorUrl>https://weeblr.com/</authorUrl>
<version>1.0.0</version>
<description>PLG_FORSEF_SAMPLE_XML_DESCRIPTION</description>
<files>
<filename plugin="sample">sample.php</filename>
<folder plugin="sample">plugin</folder>
</files>
</extension>

以下是 Joomla 插件的主文件 sample.php:

<?php
/**
* Project: 4SEF
*
* @package 4SEF
* @copyright Copyright Weeblr llc - 2022
* @author Yannick Gaultier - Weeblr llc
* @license GNU General Public License version 3; see LICENSE.md
* @version 1.0.0
* @date 2022-06-03
*/
defined('_JEXEC') or die;
use Weeblr\Forsef\Factory as ForsefFactory;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Event\DispatcherInterface;
/**
* 4SEF - A loader for a 4SEF SEF URL plugin.
*
* @since 1.0.0
*/
class PlgForsefSample extends CMSPlugin
{
/**
* @var string location of plugin file from
*/
private $pluginFileName = '/plugin/sample.php';
/**
* Constructor
*
* @param DispatcherInterface &$subject The object to observe
* @param array $config An optional associative array of configuration settings.
* Recognized key values include 'name', 'group', 'params', 'language'
* (this list is not meant to be comprehensive).
*
* @since 1.5
*/
public function __construct(&$subject, $config = array())
{
parent::__construct($subject, $config);
ForsefFactory::get()->getThe('hook')->add(
'forsef_on_load_plugins',
function () {
$filename = __DIR__ . $this->pluginFileName;
if (file_exists($filename))
{
include_once $filename;
}
}
);
}
}