Page 1 of 1
Using Route_Action server event to add user-defined routes (v2021)
Posted: Mon Aug 16, 2021 10:46 am
by mobhar
I've seen Using Route_Action server event (v2021). The code in Route_Action server event from that link is used to display the content without header and footer.
Let's see the example from demo2021 for the models table from that file:
// models
$app->any('/modelslist[/{ID}]', ModelsController::class . ':list')->add(PermissionMiddleware::class)->setName('modelslist-models-list'); // list
...
$app->group(
'/models',
function (RouteCollectorProxy $group) {
$group->any('/' . Config("LIST_ACTION") . '[/{ID}]', ModelsController::class . ':list')->add(PermissionMiddleware::class)->setName('models/list-models-list-2'); // list
...
}
);
That code will allow us to use two different URL as follows:
app/modelslist
app/models/list
Now, we want to define our own another route, as follows:
app/data-model/list
So, at the moment we have to add the additional code below that one above:
$app->group(
'/data-model',
function (RouteCollectorProxy $group) {
$group->any('/' . Config("LIST_ACTION") . '[/{ID}]', ModelsController::class . ':list')->add(PermissionMiddleware::class)->setName('models/list-models-list-2'); // list
...
}
);
Re: Server event to add user-defined routes
Posted: Mon Aug 16, 2021 11:07 am
by arbei
mobhar wrote:
The code in Route_Action server event from that link is used to display the content without header and footer.
That is just an example of use. The server event actually allows you to add any routes because the $app
object is passed to it. You may simply add your code in the event, e.g.
// Route Action event
function Route_Action($app)
{
$app->group(...);
}
Re: Server event to add user-defined routes
Posted: Mon Aug 16, 2021 11:16 am
by mobhar
I did, here is the code I added into Route_Action server event:
$app->group(
'/data-model',
function (RouteCollectorProxy $group) {
$group->any('/' . Config("LIST_ACTION") . '[/{ID}]', ModelsController::class . ':list')->add(PermissionMiddleware::class)->setName('data-model/list-models-list-3'); // list
}
);
but it raised an error as follows:
Fatal error: Uncaught TypeError: Argument 1 passed to Closure::PHPMaker2021\demo2021\{closure}() must be an instance of PHPMaker2021\demo2021\RouteCollectorProxy, instance of Slim\Routing\RouteCollectorProxy given, called in D:\wamp\www\demo2021_original\vendor\slim\slim\Slim\Routing\RouteGroup.php on line 75 and defined in D:\wamp\www\demo2021_original\src\userfn.php on line 143
( ! ) TypeError: Argument 1 passed to Closure::PHPMaker2021\demo2021\{closure}() must be an instance of PHPMaker2021\demo2021\RouteCollectorProxy, instance of Slim\Routing\RouteCollectorProxy given, called in D:\wamp\www\demo2021_original\vendor\slim\slim\Slim\Routing\RouteGroup.php on line 75 in D:\wamp\www\demo2021_original\src\userfn.php on line 143
Re: Server event to add user-defined routes
Posted: Mon Aug 16, 2021 11:29 am
by arbei
That is only PHP namespace issue, you may either remove the type RouteCollectorProxy
or specify the fully qualified name \Slim\Routing\RouteCollectorProxy
.
Re: Server event to add user-defined routes
Posted: Mon Aug 16, 2021 11:44 am
by mobhar
Thank you for giving the clue to resolve the issue.
I finally got it working. Here is my revised code in Route_Action server event:
$app->group(
'/data-model',
function (\Slim\Routing\RouteCollectorProxy $group) {
$group->any('/' . Config("LIST_ACTION") . '[/{ID}]', ModelsController::class . ':list')->add(PermissionMiddleware::class)->setName('data-model/list-models-list-3'); // list
}
);