Ga naar inhoud

Plugins van derden

4SEF bouwt SEF-URL’s voor een bepaalde component op met een specifiek stuk code, op maat van die component. Het biedt ondersteuning voor alle eigen inhoudstypen van Joomla, plus enkele veelgebruikte extensies van derden.

Voor componenten zonder specifieke ondersteuningsplugin valt 4SEF terug op het eigen bestand router.php van de component, dat normaal bedoeld is voor het systeem voor SEF-URL’s van Joomla. Dit proces is automatisch en werkt in de meeste gevallen.

Als u een verfijndere set URL’s wilt bieden voor een component die u gebruikt of hebt gebouwd, kunt u daarvoor een SEF-URL-driver voor 4SEF maken.

Een driver is minimaal één PHP-klassebestand, dat u kunt opslaan waar u maar wilt. Het kan bijvoorbeeld deel uitmaken van uw eigen component, of in een specifieke plugin staan (zie de paragraaf hieronder, Uw driver laden).

De driver breidt een basisklasse van 4SEF uit en heeft 3 hoofdtaken:

  • de SEF-URL opbouwen op basis van de niet-SEF-variabelen, vergelijkbaar met het eigen bestand router.php van Joomla
  • de “genormaliseerde” niet-SEF-URL opbouwen. Het meeste daarvan doet 4SEF al, maar u wilt misschien enkele componentspecifieke queryvariabelen verwijderen.
  • optioneel bepalen of een SEF-URL als niet-SEF moet worden gelaten, wat soms handig is en ruimte bespaart
<?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;
}
}

Omdat uw driver gewoon een specifieke PHP-klasse is, detecteert en gebruikt 4SEF hem automatisch wanneer dat nodig is, zolang dit PHP-bestand één keer wordt geladen.

Er zijn 3 manieren om dat te doen, zoals hieronder beschreven. We raden aan een echte Joomla-plugin te bouwen rond uw driver: dat is de handigste manier voor de eindgebruikers, voor updates enz., hoewel alle 3 de opties perfect bruikbaar zijn.

U kunt gewoon ergens in uw eigen extensie een include_once 'my_4sef_driver.php' uitvoeren om de driver beschikbaar te maken. Als hij de juiste naam heeft (zie het voorbeeld hierboven), kan 4SEF hem detecteren en gebruiken.

Reageren op de laadgebeurtenis van de plugins van 4SEF

Section titled “Reageren op de laadgebeurtenis van de plugins van 4SEF”

Vlak voordat 4SEF zijn eerste SEF-URL moet opbouwen, activeert het een gebeurtenis waarmee u uw driver kunt laden. U kunt uw driver laden door de volgende code aan uw extensie toe te voegen:

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

U kunt een echte Joomla-plugin bouwen om uw driver te verpakken en aan gebruikers te leveren. Dat is eigenlijk vrij eenvoudig en zorgt ervoor dat uw driver vroeg genoeg wordt geladen.

Hier is de structuur van zo’n plugin:

Mapstructuur van een URL-plugin voor 4SEF

Het bestand /plugin/sample.php bevat uw driver, zoals beschreven in de paragraaf Voorbeeldcode van een driver hierboven.

Hier is het manifestbestand, 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>

en hier is het hoofdbestand van de Joomla-plugin, 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;
}
}
);
}
}