PHP 8.3.4 Released!

Yaf_Router::getCurrentRoute

(Yaf >=1.0.0)

Yaf_Router::getCurrentRouteRécupère le nom effectif de la route

Description

public Yaf_Router::getCurrentRoute(): string

Récupère le nom de la route actuellement en utilisation dans le processus routeur.

Note:

Vous devriez appeler cette méthode une fois le processus routeur terminé, sachant qu'avant la fin du processus, cette méthode retourne toujours null.

Liste de paramètres

Cette fonction ne contient aucun paramètre.

Valeurs de retour

Le nom de la route effective, sous la forme d'une chaîne de caractères.

Exemples

Exemple #1 Enregistre quelques routes dans le Bootstrap

<?php
class Bootstrap extends Yaf_Bootstrap_Abstract{
public function
_initConfig() {
$config = Yaf_Application::app()->getConfig();
Yaf_Registry::set("config", $config);
}

public function
_initRoute(Yaf_Dispatcher $dispatcher) {
$router = $dispatcher->getRouter();
$rewrite_route = new Yaf_Route_Rewrite(
"/product/list/:page",
array(
"controller" => "product",
"action" => "list",
)
);

$regex_route = new Yaf_Route_Rewrite(
"#^/product/info/(\d+)",
array(
"controller" => "product",
"action" => "info",
)
);

$router->addRoute('rewrite', $rewrite_route)->addRoute('regex', $regex_route);
}

/**
* Enregistre un plugin
*/
public function __initPlugins(Yaf_Dispatcher $dispatcher) {
$dispatcher->registerPlugin(new DummyPlugin());
}
}
?>

Exemple #2 plugin Dummy.php (sous le dossier application.directory/plugins)

<?php
class DummyPlugin extends Yaf_Plugin_Abstract {
public function
routerShutdown(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {
var_dump(Yaf_Dispatcher::getInstance()->getRouter()->getCurrentRoute());
}
}
?>

Résultat de l'exemple ci-dessus est similaire à :

/* Pour http://yourdomain.com/product/list/1
 * DummyPlugin affichera :
 */
string(7) "rewrite"

/* Pour http://yourdomain.com/product/info/34
 * DummyPlugin affichera :
 */
string(5) "regex"

/* Pour toutes les autres requêtes URI
 * DummyPlugin affichera :
 */
string(8) "_default"

Voir aussi

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top