PHP 8.4.6 Released!

Yaf_Route_Rewrite::__construct

(Yaf >=1.0.0)

Yaf_Route_Rewrite::__constructYaf_Route_Rewrite constructor

Description

public Yaf_Route_Rewrite::__construct(string $match, array $route, array $verify = ?)

Parameters

match

A pattern, will be used to match a request uri, if it doesn't match, Yaf_Route_Rewrite will return false.

You can use :name style to name segments matched, and use * to match the rest of the URL segments.

route

When the match pattern matches the request uri, Yaf_Route_Rewrite will use this to decide which module/controller/action is the destination.

Either of module/controller/action in this array is optional, if you don't assign a specific value, it will be routed to default.

verify

Return Values

Examples

Example #1 Yaf_Route_Rewrite example

<?php
/**
* Add a rewrite route to Yaf_Router route stack
*/
Yaf_Dispatcher::getInstance()->getRouter()->addRoute("name",
new
Yaf_Route_rewrite(
"/product/:name/:id/*", //match request uri leading "/product"
array(
'controller' => "product", //route to product controller,
),
)
);
?>

The above example will output something similar to:

/* for http://yourdomain.com/product/foo/22/foo/bar
 * route will result in following values:
 */
array(
  "controller" => "product",
  "module"     => "index", //(default)
  "action"     => "index", //(default)
)

/**
 * and request parameters:
 */
array(
  "name" => "foo",
  "id"   => 22,
  "foo"  => bar
)

Example #2 Yaf_Route_Rewrite example

<?php
/**
* Add a rewrite route to Yaf_Router route stack by calling addconfig
*/
$config = array(
"name" => array(
"type" => "rewrite", //Yaf_Route_Rewrite route
"match" => "/user-list/:id", //match only /user/list/?/
"route" => array(
'controller' => "user", //route to user controller,
'action' => "list", //route to list action
),
),
);
Yaf_Dispatcher::getInstance()->getRouter()->addConfig(
new
Yaf_Config_Simple($config));
?>

The above example will output something similar to:

/* for http://yourdomain.com/user-list/22
 * route will result in following values:
 */
array(
  "controller" => "user",
  "action"     => "list",
  "module"     => "index", //(default)
)

/**
 * and request parameters:
 */
array(
  "id"   => 22,
)

Example #3 Yaf_Route_Rewrite (as of 2.3.0) example

<?php
/**
* Add a rewrite route use match result as m/c/a name
*/
$config = array(
"name" => array(
"type" => "rewrite",
"match" => "/user-list/:a/:id", //match only /user-list/*
"route" => array(
'controller' => "user", //route to user controller,
'action' => ":a", //route to :a action
),
),
);
Yaf_Dispatcher::getInstance()->getRouter()->addConfig(
new
Yaf_Config_Simple($config));
?>

The above example will output something similar to:

/* for http://yourdomain.com/user-list/list/22
 * route will result in following values:
 */
array(
  "controller" => "user",
  "action"     => "list",
  "module"     => "index", //(default)
)

/**
 * and request parameters:
 */
array(
  "id"   => 22,
)

See Also

User Contributed Notes

aqueel at ikonami dot net
12 years ago
Working Example for Modules in YAF FOR PHP

Please follow these steps to make modules work in YAF.

1. Create a folder with name modules inside application directory the path would normally look like this

/application/modules

2. In modules folder copy following folders and files from application root.
i. conf / configration folder ( what ever name your configration folder has)
ii. controllers
iii. models
iv. plugins ( if you have)
v. Views

Now your folder structure will look like this

-- application
-- controllers
-- models
-- modules
-- [moduledirectory]
-- controllers
-- models
-- plugins
-- views
-- plugins
-- views

application.ini file in configuration folder will look like this the only thing to notice closely in this file is this line

;defined modules
application.modules= "Index,director" // comma separated list of all modules that you will use in your web application using yaf

############################################################
[product]
;layout
application.directory = APP_PATH
application.bootstrap = APP_PATH "Bootstrap.php"
application.library = BASE_PATH "/library"

appnamespace = "Application"
resources.frontController.controllerDirectory = APP_PATH "controllers"
resources.frontController.params.displayExceptions = 0
resources.frontController.defaultModule = "index"
resources.frontController.defaultController = "index"
resources.frontController.defaultAction = "index"
;resources.frontController.moduleDirectory = APP_PATH "modules/"
resources.layout.layoutPath = APP_PATH "/layouts/scripts/"
resources.view[] =

;errors (see Bootstrap::initErrors)
application.showErrors=0

;enable the error controller
application.dispatcher.catchException=0
application.dispatcher.defaultModule=Index
application.dispatcher.defaultController=Index
application.dispatcher.defaultAction=index

;defined modules
application.modules= "Index,director"

;database
database.adapter = Pdo_Mysql
database.params.dbname = printmaster
database.params.host = localhost ;NA when using sqlite
database.params.username = root ;NA when using sqlite
database.params.password = root ;NA when using sqlite

[devel : product]

;errors (see Bootstrap::initErrors)
application.showErrors=1

#############################################################

Add this in your bootstrap.php

<?php
public function _initRoute(Yaf_Dispatcher $dis) {

$route1 = new Yaf_Route_Rewrite("/director",
array(
"controller" => "index",
"module" => "director",
"action" => "index"

)
);

}
?>
To Top