How to create a configuration file in Laravel 4?

7

I would like to know how to create, import and use new configuration files in a Laravel 4 project.

By this, I mean, project files and settings themselves. For example: I would like to create a file that has some static project information, without the need to persist them in the database.

Example:

<?php 
  $config = array(
    'titulo_padrao' => 'Site do João',
    'meta_keywords' => 'palavra1, palavra2, palavra3',
    'meta_description' => 'Dramatically repurpose covalent niches vis-a-vis resource sucking benefits. Authoritatively productize.',
    'script_ga' => 'script_aqui',
    'grupo_administradores' => array(1,2,3),
    'grupo_usuarios' => array(4,5),
    'etc' => '...'
  );
?>
    
asked by anonymous 16.12.2013 / 11:46

5 answers

11

You can create files in the settings folder, nome_do_seu_arquivo.php , and access the properties via:

Config::get('nome_do_seu_arquivo.nome_da_propriedade');

Or:

Config::get('nome_do_seu_arquivo.nome_do_array_da_propriedade.nome_da_propriedade');
    
16.12.2013 / 11:50
7

Create a php file inside the app/config/myconfig.php folder (it can be any name):

<?php 
  return  array(
    'titulo_padrao' => 'Site do João',
    'meta_keywords' => 'palavra1, palavra2, palavra3',
    'meta_description' => 'Dramatically repurpose covalent niches vis-a-vis resource sucking benefits. Authoritatively productize.',
    'script_ga' => 'script_aqui',
    'grupo_administradores' => array(1,2,3),
    'grupo_usuarios' => array(4,5),
    'etc' => '...'
  );

To access information, use:

Config::get('myconfig.titulo_padrao');

For more information link

    
16.12.2013 / 11:52
1

I did so when I needed it:

<?php
/*
|---------------------------------------------------
| File: application/config/_sysvars.php
|---------------------------------------------------
|
| Configurações estáticas para uso no site
*/

return array(
    //=========== Configurações principais===========//
    //Email cadastrado no PagSeguro e outros sistemas de recebimento.
    'email_pagseguro' => "[email protected]",
    //Identificador Paypal
    'email_paypal' => "[email protected]",
    //Identificador Moip
    'email_moip' => "[email protected]",
    //Site Title:
    'title' => 'Dummy Hans - LARAVEL',
);
?>

No controller:

<?php
/*
|---------------------------------------------------
| File: application/controller/HomeController.php
|---------------------------------------------------
|
| Controler para a Home.
*/
class HomeController extends BaseController {

    /* The layout that should be used for responses.
     */
    protected $layout = 'layouts.master';

    public function showHome() {

        $sysvars = Config::get('_sysvars'); //esta na pasta app/config/_sysvars.php

        foreach ($sysvars as $key => $value ) {
            $data["$key"]=$value;
        }
        //dd($data);

        //Renderiza a view v_home
        $this->layout->content = View::make('v_home',$data);
    }
}
?>
    
16.12.2013 / 12:17
0

Note that to change these settings, Config::set() will not save the file as expected.

You will have to implement this on your own, and in case of configuration files in PHP where you normally want to preserve possible comments, this can become complicated. If there are no comments, var_export() will suffice.

If you want to preserve comments (something desirable when changing Laravel configuration files, for example), we recommend using the Laravel Setting , installable by Composer itself. It allows you to create, read, change, and delete your own configuration directives through JSON files, as well as fallback for default configuration directives (PHP files).

    
22.12.2013 / 22:20
0

Complementing the answers, for those who do not faithfully follow the standard framework of the Framework. Well, I have a "domain" folder of the application, which inherits its name, for example:

- app
    - Acme
        - Config
        - Models
        - Repositories
        - Services
        - Queues
        - ...
    - storages
    - tests
    - config        
    - ...

Notice that within Acme I have a Config folder. This way, I can separate the settings of the components of the Framework with those of the logic of my application. app/config is maintained, with all default settings.

To make the Framework understand that Acme/Config is a namespace of "configuration files", you can add the following line at the end of the app/start/global.php file:

/*
|--------------------------------------------------------------------------
| Acme Config Namespace
|--------------------------------------------------------------------------
*/

Config::addNamespace('Acme', app_path('Acme/Config'));

So, let's say you have this configuration file:

- app
    - Acme
        - Config
            - social.php

You can access it, for example, as follows:

\Config::get('Acme::social.facebook');

Acme:: is to indicate the namespace where the Framework needs to look for the configuration file. social is the name of the file and facebook an array key.

    
17.12.2013 / 01:05