Ok so I have 4 folders all which have their own route.php. So I would like to require the path to each folder based upon the uri path. So for example, if my website path is www.example.com/user then the Slim framework would require the path to controller/users/routes. I'm trying to achiee this using middleware but when I test it I get a "Call to a member function error" so how do I fix this.
Here is my code below:
//determine the uri path then add route path based upon uri
$app->add(function (Request $request, Response $response, $next) {
if (strpos($request->getAttribute('route'), "/user") === 0) {
require_once('controllers/users/routes.php');
} elseif (strpos($request->getUri()->getPath(), "/public") === 0) {
require_once('controllers/public/routes.php');
} elseif (strpos($request->getUri()->getPath(), "/brand") === 0) {
require_once('controllers/brands/routes.php');
}elseif (strpos($request->getUri()->getPath(), "/admin") === 0) {
require_once('controllers/admin/routes.php');
}elseif (strpos($request->getUri()->getPath(), "/") === 0) {
require_once('routes.php');
}
$response = $next($request, $response);
return $response;
});
So before anything the framework determines the route then adds the required path. But something is not functioning right, any ideas?