How to insert a file into an array

Asked

Viewed 95 times

2

I’m having a hard time and I couldn’t fix it. I have a settings file in this file I have an array and several arrays within it and one of these is my route array. What I need to do is include a file within this route array because I have many routes and I want to separate them by file for easy maintenance later. well, what I got is this:

Routes.php

return array(
    'dependencias' => [
        // array de dependências
    ],

    'routes' => [
        // array de rotas que eu quero inserir
    ]

);

php routes.

[
'name' => 'home',
'path' => '/',
'middleware' => Site\Action\HomePageAction::class,
'allowed_methods' => ['GET'],
],

what I need is to include the.php routes file inside the Routes array. Has anyone done this or knows how to do it?

Grateful

1 answer

0

Normally includes the file in the array of configuration file.

Routes.php

return [
    'routes' => [
        'minhasRotas' => require "rotas.php",
    ]
];

Sets that your file will return one array.

php routes.

return [
    [
        'name' => 'home',
        'path' => '/',
        'middleware' => Site\Action\HomePageAction::class,
        'allowed_methods' => ['GET'],
    ],
    [
        'name' => 'login',
        'path' => '/login',
        'middleware' => Site\Action\LoginAction::class,
        'allowed_methods' => ['GET'],
    ],
    [...],
    [...],
];
  • Oops. Thank you for answering... right.. for a route actually worked, but now my problem is that I need to put several routes inside a file, for example: in this file routes, will be the login routes so will have the login route, auth, admin and logout.. how do I return an array of each route? I tested by placing each route between [ ] but it didn’t work :(

  • Oops, see if the change I made addresses your doubt.

  • I would have done it, but it didn’t work.. because you can only have an array inside the route and then the arrays with the routes and so there is a more array before the array with the routes. I don’t know what to do..

  • I managed to do as I wanted using the array_merge(), it joins several arrays within one.. Thank you all

Browser other questions tagged

You are not signed in. Login or sign up in order to post.