PHP 8.6.0 Beta 3 is available for testing

Yaf_Router::getCurrentRoute

(Yaf >=1.0.0)

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

Description

public function Yaf_Router::getCurrentRoute(): string

Récupère le nom de la route qui est effective dans le processus de routage.

Note:

Il est recommandé d'appeler cette méthode une fois le processus de routage terminé, car avant cela, cette méthode retournera toujours null.

Liste de paramètres

Cette fonction ne contient aucun paramètre.

Valeurs de retour

Une chaîne de caractères, le nom de la route effective.

Exemples

Exemple #1 Enregistrement de 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 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 toute autre URI de requête
 * DummyPlugin affichera :
 */
string(8) "_default"

Voir aussi

add a note

User Contributed Notes

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