Laravel routing woes -
i have routing working great:
route::get('/', 'maincontroller@index'); route::get('about', 'maincontroller@about'); route::get('contact', 'maincontroller@contact'); route::get('signup', 'maincontroller@signup');
the correct functions in maincontroller.php
called expected.
where i'm having trouble in getting following work:
i have new file called apicontroller.php
. requests http://eamorr.com/api/getusers should handled getusers()
in apicontroller.php
.
i tried this:
route::get('api', 'apicontroller@index'); //this works fine... route::any('api/{$function}', function($function){ //but won't work! return redirect()->route('api/'.$function); });
i don't want list every function like:
route::get('api/adduser', 'apicontroller@adduser'); route::get('api/getuser', 'apicontroller@getuser'); route::get('api/getallusers', 'apicontroller@getallusers'); ...
i'd prefer if /api/*
requests directed apicontroller...
if has tips, great...
i started learning laravel yesterday, please go easy on me!
you call controller action this:
route::any('/api/{action}', function($action) { // call method controller class , return it's response return app()->make('app\http\controllers\apicontroller')->callaction($action, []); });
however suggest implicit controllers @shaddy suggested in answer, because actions such adduser
require restricting http verb post
, can't approach.
also, since route path looks you're building api, might want using restful resource controllers.
Comments
Post a Comment