Laravel dependency for views

I am trying to get injection hang and IoC container in Laravel.

I currently have a class full of static methods that I use in my views. For example.

class Templatizer {
    /**
     * Generates a colored FontAwsome check or cross from a true/false argument
     * @param  boolean $bool 
     * @return string       The HTML markup of the icon
     */
    public static function boolicon($bool)
    {
        return $bool ? '<span class="fa fa-check text-success"></span>' : '<span class="fa fa-times text-danger"></span>';
    }
}

      

I have a composer for the class autoloading and in my opinion I can just go {{ Templatizer::boolicon($trueOrFalseValue) }}

.

This is clearly bad practice and I would like to avoid using static methods. I guess the correct way is to insert an instance Templatizer

and use methods like {{$ templatizer-> boolicon ($ v)}} `.

How would I structure this? Presumably I will need to inject an instance Templatizer

into my controller via the constructor? eg.

class PagesController extends BaseController {

    protected $templatizer;

    public function __construct(Templatizer $templatizer)
    {
        $this->templatizer = $templatizer;
    }
}

      

And then, say for the index page method, I pass the instance to the view? eg.

# inside PagesController
public function index()
{
    return View::make('pages.index', ['templatizer' => $this->templatizer]);
}

      

If that's correct, where is the appropriate place to host my class Templatizer

? How do I bind it to an IoC container?

+3


source to share


3 answers


First of all, I see nothing wrong with calling these methods statically. It looks like this is just an HTML helper and you should be fine.

If you decide to go with Dependency Injection, the "correct" way to register your class is to use Service Providers .

Assigning it to the views you wrote will work, but you can also get it when you need it:



$templatizer = App::make('Yournamespace\Templatizer');

      

Finally, perhaps the best solution for you is to create your own Facade .

+1


source


Yes, you have to inject it through the controller constructor, the files can live anywhere as long as they are loaded automatically.

I like to create a folder in my root directory called src

so my composer.json file looks like this:

"autoload": {
    "classmap": [
        ....
    ],
    "psr-4": {
        "Foo\\": "src/"
    }
}

      

Then you can get src / Templatizer.php which looks like this:

<?php namespace Foo;

class Templatizer {

}

      



Now you need a service provider to bind your Templatizer instance (this basically makes Laravel aware of your class and allows you to inject it into your controllers) in src / FooServiceProvider.php

<?php namespace Foo;

use Illuminate\Support\ServiceProvider;

class FooServiceProvider extends ServiceProvider {

    /**
     * Indicates if loading of the provider is deferred
     *
     * @var boolean
     */
    protected $defer = false;

    /**
     * Register the service provider
     */
    public function register() {
        $this->app->bind('Foo\Templatizer', function($app) {
            return new Templatizer();
        });
    }
}

      

Don't forget to add Foo\FooServiceProvider

to the array providers

in your app config and you should be set to execute ...

public function __construct(Foo\Templatizer $templatizer) {

      

+1


source


You are also instantiating on BaseController.

class BaseController extends Controller {

    /**
     * Setup the layout used by the controller.
     *
     * @return void
     */
    var $templatizer;
    protected function setupLayout()
    {
        if ( ! is_null($this->layout))
        {
            $this->layout = View::make($this->layout);
            $templatizer = new \Templatizer();
            View::share('templatizer', $templatizer);
        }
    }

}

      

use this instance $templatizer

throughout the view. something like {{$ templatizer-> boolicon ($ v)}} `.

0


source







All Articles