An Phan
An Phan

Reputation: 2568

Where to store global data?

I'm writing this small blogging platform in Laravel 4.1, for learning purpose. My first obstacle is, I don't know where the global data (ie. dynamic blog settings, plugin and theme array) should be put, so that I can access them from anywhere (similar to how $wpdb in WordPress works - we only need global $wpdb; to access it).

Of course $GLOBALS works, but we know that it's evil and should be avoided. Also, I'd like everything to be as Laravel'y as possible.

Have tried:

App::bind('settings', []); // error
App::instance('settings', []); // how to populate and retrieve it back?

Any ideas? Thanks in advance.

Upvotes: 1

Views: 557

Answers (2)

Miroslav Trninic
Miroslav Trninic

Reputation: 3455

Well, global in a Laravel context means relative to Application, if you are not writing anything that "lives" outside of Application context. There are couple of ways in which you can make you data available to application. But first, you have to make decision what kind of "globals" do you need ?

First and most important rule is that any change that you make besides Application specific folder structure, has to be told to composer.

Second, without making any changes, Laravel has default locations for most important parts of any "ordinary" web application.

Application folder structure is pretty self descriptive, but in short:

Configurations belongs to app/config
Models to app/models
Views to app/views
Controllers to app/controllers
Database to app/database
Routes to app/routes.php

If you follow this basic structure, you can create amazing web-apps. these folders and files are already namespaced and classes inside them are auto resolved. In you example you were trying to bind something to application, which is redundant in you case. Bindings to container are used in cases when you want to add some new classes and functionality to existing structure.

In short:

create you first route in routes.php like this:

Route::get("GET",function(){
    return "my first route";
})

And from that point follow basic MVC flow, which is no different then most frameworks. Laravel is talking to you, just open you ears :) And if you stack, just ask here, somebody will help.

Upvotes: 0

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87719

If this is not something you need to keep between sessions, you can use:

Config::set('myglobals.name', 'An Phan');

And then

var_dump( Config::get('myglobals.name') );

But you also have to think a bit and think why would you need globals in the first place. Take a look at this: http://c2.com/cgi/wiki?GlobalVariablesAreBad.

EDIT

This is not a workaround, this is something Laravel provides out of the box and you can use yourself. Usually the purpose of Config would be having configuration files to be used by your application, but sometimes you just need to change those values during the request, that's why Laravel provides also a set() method.

Unfortunately WP has an old codebase and if you are trying do things the way WP does things, you're goind the in wrong path.

What would be the Laravel way depends on what you're trying to accomplish with your project, so you'll have to tell a little bit more about it.

The mindset to start with is: "I don't need globals" and when you get to a point where a global is needed, you ask yourself "how do I do this without using a global?".

Usually, you just need global values if you have settings to store. If it's something that you have to use to set a state during a request, you need to use objects. You can have global objects in Laravel, you can have singletons (objects that has only one instance in the whole application), you can create properties objects:

class SidebarProperties {

    private $width;

    public function __construct($width)
    {
       $this->width = $width
    }

    public getWidth()
    {
       return $this->width;
    }

}

So you have an uncountable number of way to not use globals, you just have to think about your project and pick the one is best at that moment.

Upvotes: 1

Related Questions