2

Trying to call a method on a controller but this wont work on AltoRouter

$router->map( 'GET', '/users/[i:id]/', 'UserController#showDetails' );

What I'm doing wrong?

PS:There is no example on how to get the parameters on the Controller method either.

Gabriel Rodriguez
  • 1,163
  • 10
  • 23

1 Answers1

1

The route is correct - assuming the UserController is in the global namespace. If not, make sure to use the fully qualified namespace in the string.

$router->map( 'GET', '/users/[i:id]/', 'App\UserController#showDetails' );

Then, in your controller, you will access the parameter like this:

public function showDetails ($id) {

}

The [i:id] means to match an integer as a variable named id.

swatkins
  • 13,530
  • 4
  • 46
  • 78
  • I'm really lost, I can use Laravel routes and everything but with AltoRouter I can only make to work a callable function ie: `$router->map( 'GET', '/hello', function() {....`...can you please post the full scripts – Gabriel Rodriguez Mar 25 '16 at 18:41
  • Should I use `call_user_func_array( $match['target'], $match['params'] ); ` or `require $match['target'];` ?? – Gabriel Rodriguez Mar 25 '16 at 18:42
  • Can you please post all the files......index.php....UserContorller.php and their specific path – Gabriel Rodriguez Mar 25 '16 at 18:43
  • why are you not using laravel routes in a laravel project? – swatkins Mar 25 '16 at 19:01
  • At the top of your `UserController` script, you should have a namespace. Something like `namespace App\Http\Controllers;` If that is the case, then you will access it like this: `App\Http\Controllers\UserController#showDetails` -- If not, then you will access it like this: `\UserController#showDetails`. – swatkins Mar 25 '16 at 19:06
  • Is not a laravel project...I just mentioned laravel in the sense that I've used Laravel before in other projects. – Gabriel Rodriguez Mar 25 '16 at 19:09
  • Ok I added the namespace on the controller file but still no success. On the routes index.php file If I put it that way `App\Http\Controllers\UserController#showDetails` I get `Fatal error: require(): Failed opening required...` If I change it to `App\Http\Controllers\UserController.php` theres no error but nothing happens – Gabriel Rodriguez Mar 25 '16 at 19:17
  • If it's not a laravel project I doubt if that's your actual name space – swatkins Mar 25 '16 at 19:26
  • I just created that directory structure from your example, I didn't had any of that before. – Gabriel Rodriguez Mar 25 '16 at 19:35
  • 1
    I got it, the whole thin was simply replacing `UserController#showDetails` by `UserController::showDetails`...that is the only way `call_user_func()` can call another method inside the class. – Gabriel Rodriguez Mar 25 '16 at 20:49
  • Fair enough. I didn't have any problems with using #, but I'm glad it's working for you. – swatkins Mar 25 '16 at 20:57
  • Ummm interesting...might be the Autoloader? Im using `SplClassLoader.php`... I didn't installed AltoRouter from composer...I just got the source files and added an Class loader – Gabriel Rodriguez Mar 25 '16 at 21:05