How to create a configuration file in Laravel 4?

Asked

Viewed 1,837 times

8

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

By that, I mean, project files and own settings. For example: I would like to create a file that has some static project information, without the need to persist it 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' => '...'
  );
?>
  • The Brayan link has all the features!

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');

7

Create a php file inside the folder app/config/myconfig.php (could 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 http://laravel.com/docs/configuration

  • Note that Laravel uses the resource: $cfg = include("app/config/myconfig.php"); Return that’s in the file included.

2

I did it 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',
);
?>

On the 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);
    }
}
?>
  • can show how you did to implement Pagseguro in Laravel? so far I could not, I thank you.

  • 1

    sent well, this is easier, more you think better put in the App than follow the pattern and put in the config?

  • Today I would put in config. At the time it seemed less confusing and safe to put in the App

0

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

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

If you want to preserve the comments (something desirable when changing the Laravel configuration files, for example), it is recommended to use the package Laravel Setting, installable by Composer itself. It allows you to create, read, change and delete directives of your own settings through JSON files, even having fallback for the default settings directives (PHP files).

0

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

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

Note that inside Acme I have a folder Config. This way, I can separate the configurations of the Framework components with those of my application logic. A 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 file app/start/global.php:

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

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

Done so, let’s assume 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 search the configuration file. social is the file name and facebook an array key.

Browser other questions tagged

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