コード例 #1
0
ファイル: route.inc.php プロジェクト: 42mate/towel
/**
 * Ads a route in the application.
 *
 * @param $method : GET, POST, PUT, DELETE
 * @param $route : The route Pattern (Any Silex Route Valid Pattern).
 * @param array $options :
 *    controller: The controller with the action
 *    action : The method or function name to execute.
 *    secure : Boolean if you require and authorized user for the action.
 *    route_name : Name of the route to be used in the url generator.
 *
 * @throws Exception if no action is provided in options.
 *
 * @return The action Result.
 */
function add_route($method, $route, $options = array())
{
    $towel = get_app();
    if (!empty($options['controller']) && !empty($options['action'])) {
        $action = array($options['controller'], $options['action']);
    } else {
        throw new Exception('Needs an action');
    }
    $route = $towel->silex()->{$method}($route, function (\Symfony\Component\HttpFoundation\Request $request) use($action, $options) {
        $controller = get_base_controller();
        if (!empty($options['secure']) && $options['secure'] == true) {
            if (!$controller->isAuthenticated()) {
                return $controller->redirect('/login');
            }
        }
        return call_user_func_array($action, array($request));
    });
    if (!empty($options['route_name'])) {
        $route->bind($options['route_name']);
    }
}
コード例 #2
0
ファイル: TowelTest.php プロジェクト: 42mate/towel
 public function testGetBaseController()
 {
     $baseController = get_base_controller();
     $this->assertInstanceOf('\\Towel\\Controller\\BaseController', $baseController, 'Is not the base controller of Towel');
 }