Tovább a tartalomhoz

Külső bővítmények

A 4SEF egy adott komponens SEF URL-jeit egy kifejezetten arra a komponensre szabott kódrészlettel építi fel. Támogatást nyújt az összes natív Joomla tartalomtípushoz, valamint néhány gyakran használt külső bővítményhez.

Azoknál a komponenseknél, amelyekhez nincs külön támogató bővítmény, a 4SEF a komponens saját router.php fájlját használja, amely általában a Joomla SEF URL-rendszere számára készült. Ez a folyamat automatikus, és a legtöbb esetben működik.

Ha egy Ön által használt vagy fejlesztett komponenshez kifinomultabb URL-készletet szeretne biztosítani, ehhez SEF URL-meghajtót (drivert) hozhat létre a 4SEF számára.

Egy meghajtó legalább egyetlen PHP-osztályfájl, amelyet bárhol tárolhat. Lehet például a saját komponense része, vagy egy erre szolgáló bővítményben is elhelyezheti (lásd alább a Meghajtó betöltése bekezdést).

A meghajtó a 4SEF által biztosított alaposztályt bővíti, és 3 fő feladata van:

  • a SEF URL felépítése a nem-SEF változók alapján, a Joomla saját router.php fájljához hasonlóan
  • a „normalizált” nem-SEF URL felépítése. Ennek nagy részét a 4SEF már elvégzi, de előfordulhat, hogy el szeretne távolítani néhány komponensspecifikus lekérdezési változót.
  • opcionálisan annak eldöntése, hogy egy SEF URL-t nem-SEF formában kell-e hagyni, ami néha hasznos, és helyet takarít meg
<?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;
}
}

Mivel a meghajtó csupán egy speciális PHP-osztály, a 4SEF automatikusan észleli és szükség esetén használja, feltéve, hogy ezt a PHP-fájlt egyszer beillesztették.

Erre 3 mód van, az alábbiak szerint. Javasoljuk, hogy a meghajtót egy valódi Joomla-bővítménybe csomagolja: ez a legkényelmesebb módszer a végfelhasználók számára, a frissítések stb. szempontjából, bár mind a 3 lehetőség tökéletesen használható.

Az osztály beillesztése a saját bővítményébe

Szekció neve “Az osztály beillesztése a saját bővítményébe”

Egyszerűen elhelyezhet egy include_once 'my_4sef_driver.php' utasítást a saját bővítményében bárhol, hogy a meghajtó elérhető legyen. Ha a neve megfelelő (lásd a fenti mintát), a 4SEF észleli és használni tudja.

Reagálás a 4SEF bővítménybetöltési eseményére

Szekció neve “Reagálás a 4SEF bővítménybetöltési eseményére”

Közvetlenül azelőtt, hogy az első SEF URL-t fel kellene építenie, a 4SEF kivált egy eseményt, amellyel lehetővé teszi a meghajtó beillesztését. A meghajtót úgy töltheti be, hogy a következő kódot hozzáadja a bővítményéhez:

<?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;
}
}
);

Készíthet egy valódi Joomla-bővítményt, amely becsomagolja és eljuttatja a meghajtót a felhasználókhoz. Ez valójában elég egyszerű, és gondoskodik arról, hogy a meghajtó elég korán betöltődjön.

Egy ilyen bővítmény szerkezete a következő:

Egy 4SEF URL-bővítmény könyvtárszerkezete

A /plugin/sample.php fájl tartalmazza a meghajtót, ahogyan azt a fenti Meghajtó mintakód bekezdés leírja.

Íme a manifest fájl, a 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>

és íme a Joomla-bővítmény fő fájlja, a 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;
}
}
);
}
}