Closed simp-lee closed 2 years ago
Hi! I think that using middleware will be the best solution for your task:
// somewhere among other controllers:
class ArticleController extends BaseController
{
public function update (Request $request): string
{
$id = $params['id'];
$request->get('articleData');
// do update ...
}
}
// create router
$router = new Router();
// register middleware for all routes
$router->registerMiddleware('*', function (string $route, array $parameters){
return [Request::createFromGlobals()];
});
// additing routes
$router->addRoute('/article/update/[i:id]', [new ArticleController(), 'update'], 'POST');
// calling route
$router->callRoute($_SERVER['REQUEST_URI']);
Hope this this answer is worth of your star for repo )
Yes, star is of course : )
But I still need help. How to get the parameter ID which was defined in the addRoute() method?
// somewhere among other controllers:
class ArticleController extends BaseController
{
public function update (Request $request): string
{
$id = $params['id']; // Obviously not
$request->get('articleData');
// do update ...
}
}
Then try this:
// somewhere among other controllers:
class ArticleController extends BaseController
{
public function update (Request $request, $parameters): string
{
$id = $parameters['id'];
$request->get('articleData');
// do update ...
}
}
// register middleware for all routes
$router->registerMiddleware('*', function (string $route, array $parameters){
return [
Request::createFromGlobals(), $parameters
];
});
Hey Alex,
Thanks for your quick answer, I got your point, and she works fine now.
You are welcome )
Hi, thanks for your good job.
I want to get the data submitted by POST request, and I use Symfony\Component\HttpFoundation.
How to inject Symfony\Component\HttpFoundation into my controller?
Could you please show me some examples?
router.php
App\Admin\Controllers: