Using Route_Action server event to add user-defined routes (v2021)

Tips submitted by PHPMaker users
Post Reply
mobhar
User
Posts: 11660

Using Route_Action server event to add user-defined routes (v2021)

Post 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
        ...
    }
);

arbei
User
Posts: 9286

Post 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(...);
}

mobhar
User
Posts: 11660

Post 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


arbei
User
Posts: 9286

Post by arbei »

That is only PHP namespace issue, you may either remove the type RouteCollectorProxy or specify the fully qualified name \Slim\Routing\RouteCollectorProxy.


mobhar
User
Posts: 11660

Post 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
       }
);

Post Reply