I need feedback on my Bootstrap for the MVC architecture that iI follow. I load the routes via yaml here. Here is an example:
And here is my Bootstrap class:
I load the controllers and services via yaml,. I'm interested if you would change anything and if you see any possible issues that this kind of structure might have.
I need feedback on my Bootstrap for the MVC architecture that i follow. I load the routes via yaml here is an example:
And here is my Bootstrap:
I load the controllers and services via yaml, I'm interested if you would change anything and if you see any possible issues that this kind of structure might have.
I need feedback on my Bootstrap for the MVC architecture that I follow. I load the routes via yaml. Here is an example:
And here is my Bootstrap class:
I load the controllers and services via yaml. I'm interested if you would change anything and if you see any possible issues that this kind of structure might have.
Improving my bootstrap class (MVC)
I need feedback on my Bootstrap for the MVC architecture that i follow. I load the routes via yaml here is an example:
feed:
path: /{controller}/{action}{slash}
requirements:
id: "[1-9][0-9]*"
slash: "[/]{0,1}"
methods: [GET,POST]
#Explore Controller
explore:
path: /{controller}{slash}
requirements:
id: "[1-9][0-9]*"
slash: "[/]{0,1}"
methods: [GET,POST]
And here is my Bootstrap:
class Bootstrap
{
public function __construct()
{
$request = Request::createFromGlobals();
$locator = new FileLocator(__DIR__ . '/../../../config');
// DI container
$container = new DependencyInjection\ContainerBuilder;
$loader = new DependencyInjection\Loader\YamlFileLoader($container, $locator);
$loader->load('config-development.yml');
$container->compile();
// routing
$loader = new Routing\Loader\YamlFileLoader($locator);
$context = new Routing\RequestContext();
$context->fromRequest($request);
$matcher = new Routing\Matcher\UrlMatcher(
$loader->load('routing.yml'),
$context
);
try{
$parameters = $matcher->match($request->getPathInfo());
foreach ($parameters as $key => $value) {
$request->attributes->set($key, $value);
}
$command = $request->getMethod() . $request->get('action');
$resource = "controller.{$request->get('controller')}";
$controller = $container->get($resource);
$data = $controller->{$command}($request);
}catch(\Error $e){
$data = [
'status'=>404,
'message'=>'Not found',
'info'=>$e->getMessage()
];
}catch(\Symfony\Component\Routing\Exception\MethodNotAllowedException $e){
$data = [
'status'=>404,
'message'=>'Not found',
'info'=>$e->getMessage()
];
}catch(ResourceNotFoundException $e){
$data = [
'status'=>404,
'message'=>'Not found',
'info'=>$e->getMessage()
];
}
if(is_array($data)){
$response = new JsonResponse($data);
}else{
$response = new Response($data);
}
//Set cors headers
$response->headers->set('Access-Control-Allow-Origin', '*');
$response->headers->set('Access-Control-Allow-Methods', 'GET,HEAD,OPTIONS,POST,PUT,DELETE');
$response->headers->set('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
$response->headers->set('Access-Control-Allow-Credentials', 'true');
$response->send();
}
I load the controllers and services via yaml, I'm interested if you would change anything and if you see any possible issues that this kind of structure might have.
I'm seeking for improvements in my code.