Hi all,
How to add more php files in index.php file.
For example:
$routes = require __DIR__ . '/../app/routes.php';
$routes($app);
There is two line written in index.php to call the routes.php file.
The Same thing, I want to add another php file like that one.
I tried the same method. But did not worked.
Any suggestion or help?
Thanks
Alan
May 7, 2021, 12:58pm
2
It will depends on how you write your routes.php file.
Assuming it is similar to :
<?php
return function (App $app)
{
$app->get('/route/1', ['App\Controllers\Route1Controller', 'method'])->name('route.1');
// more routes here
};
Then replicating the structure into an other-routes.php file:
<?php
return function (App $app)
{
$app->get('/route/2', ['App\Controllers\Route2Controller', 'method'])->name('route.2');
// more routes here
};
Would allow you in your index.php :
(require __DIR__ . '/../app/routes.php')($app);
(require __DIR__ . '/../app/other-routes.php')($app);
of the more verbose way:
$routes = require __DIR__ . '/../app/routes.php';
$routes($app);
$other_routes = require __DIR__ . '/../app/other-routes.php';
$other_routes($app);
Hi sir, Thanks for your response.
But I exactly can’t get it what you trying to do say
Here is my routes.php
file
return function (App $app)
{
$app->options('/{routes:.*}', function (Request $request, Response $response)
{
// CORS Pre-Flight OPTIONS Request Handler
return $response;
});
Here is my new new php file called mobileregistration.php
return function (App $app)
{
$app->options('/{routes:.*}', function (Request $request, Response $response)
{
// CORS Pre-Flight OPTIONS Request Handler
return $response;
});
In index.php
file :
$routes = require __DIR__ . '/../app/routes.php';
$routes($app);
$routes1 = require __DIR__ . '/../app/mobileregistration.php';
$routes1($app);
Here is my coding in both routes.php and mobileregistration.php file.
But Not worked and the following error will be appear.
ResponseError. Reason: Internal Server Error, Response: {
"statusCode": 500,
"error": {
"type": "SERVER_ERROR",
"description": "Cannot register two routes matching \"\/slim_project\/public\/(.*)\" for method \"OPTIONS\""
}
}
In the mobileregistration.php file, even I changed mobileregistration instead of routes in this place :
` $app->options('/{routes:.*}', function (Request $request, Response $response)`
But did not worked.
Anyone Please tell me the correct configuration.
Thanks.
Alan
May 8, 2021, 8:08am
4
The error in this case would appear to suggest that while your code is correct in as much as calling it from index.php, you actually have two routes the same - ie both routes files register the same route:
$app->options(’/{routes:.*}’, );
Since the routing code does not permit duplicate routes - this would point to the easy solution of removing the duplicate - or at least making it conditional based on some arbitrary rule.
The other potential solution is to group all routes in one of your files under (for example) /mobile.
Hi sir,
I tried with lot of example coding but didn’t worked.
Can you please give me a coding samples for adding more php files in index.php?
And How to configure it?
Thanks