Skip to content

Instantly share code, notes, and snippets.

@nickl-
Forked from alganet/router.php
Created July 13, 2012 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickl-/3104970 to your computer and use it in GitHub Desktop.
Save nickl-/3104970 to your computer and use it in GitHub Desktop.
Another approach to multiple paths
<?php
$paths = array(
'/users',
'/users/*',
'/users/*/lists',
'/users/*/lists/*',
);
// Multiple paths handled by single callback
$r3->any($paths, function ($userId=null,$listId=null) {/***/}
))->by($processors)->when($conditions)->through($formatters)->accept($conneg);
// Multiple paths handled by single controller
$r3->any($paths, 'MyApp\Controllers\UserController'
))->by($processors)->when($conditions)->through($formatters)->accept($conneg);
$paths = array(
'/users',
'/users/*',
'/users/*/lists',
'/users/*/lists/*',
'/user-roles',
'/user-roles/*',
'/user-roles/*/lists',
'/user-roles/*/lists/*',
'/user-perms',
'/user-perms/*',
'/user-perms/*/lists',
'/user-perms/*/lists/*',
);
// Multiple paths handled by single factory
// producing different controllers based on path
$r3->any($paths, "MyApp\Controllers\iControllerInterface", array('MyApp\Controllers\ControllerFactory', 'getControllerFor')
))->by($processors)->when($conditions)->through($formatters)->accept($conneg);
@nickl-
Copy link
Author

nickl- commented Jul 13, 2012

@alganet This could be another approach to providing Router with details pertaining to the paths handled by this route.

Thinking about the hypermedia solution could be:

<?php
$r3->any('/administrators/*', function ($adminId=null) {/***/}
))->by($allMeansPossible)->linksTo($userRoute->at('/users/{adminId}');

Well if this be the case then, as fancy as it might be to know all the paths the questions still remain:

  • why do we need this info
  • what do we loose without

As this same example also works without the knowledge of the separate paths and would work as is currently:

<?php
$entitiesRoute = $r3->any('/*/*', "MyApp\Controllers\iControllerInterface", 
                            array('MyApp\Controllers\ControllerFactory', 'getControllerFor')
))->by($processors)->when($conditions)->through($formatters)->accept($conneg);

$r3->any('/administrators/*', function ($adminId=null) {/***/}
))->by($allMeansPossible)->linksTo($entitiesRoute->at('/users/{adminId}');

Resolving to the controller for users as provided by our controller factory and supplied with the adminId from administrators.
Or am I missing something.

@alganet
Copy link

alganet commented Jul 13, 2012

Possible implementation for linkTo:

    <?php
        $r3->any('/administrators/*', function ($adminId=null) {/***/}
    ))->by($allMeansPossible)->linksTo($userRoute->at('/users/*'), function($data, $link, $adminId) { // $adminId fed by ParamSynced
        $data['links'] = array("href" => $link, "title" => "User Page for {$data['name']}");
        return $data;
    });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment