lundi 18 juillet 2016

Error using two or more customs service provider - Laravel 5.1

time ago I made a facade to handle emails called 'MyMailer'.

It works good, but now, I need to add a new facade that I called 'Message'.

I made the new facade such as I made the 'MyMailer' facade. Below I will copy the code:

App/Components/MyMailer.php

<?php

namespace App\Components;

use Mail;


class MyMailer{

    public function sendMailForAlert($email, $data, $message){

        Mail::send('emails.newData', ['data' =>$data],         function($message) use ($email){
            $message->to($email)
                ->subject('Sending data');
        });

    }


}

App/Facades/MyMailer.php

<?php

namespace App\Facades;

use Illuminate\Support\Facades\Facade;

class MyMailer extends Facade {

    protected static function getFacadeAccessor()
    {
        return 'build';
    }
}

App/Providers/MyMailerServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class MyMailerServiceProvider extends ServiceProvider

{

    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        \App::bind('build', function()
    {
        return new \App\Components\MyMailer;
    });
    }
}

App/Components/MyMessage.php

<?php

namespace App\Components;


use Illuminate\Http\Request;
use Exception;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class MyMessage{

    public function getMessage()
    {
        return 'hello';
    }
}

App/Facades/MyMessage.php

<?php

namespace App\Facades;

use Illuminate\Support\Facades\Facade;

class MyMessage extends Facade {

    protected static function getFacadeAccessor()
    {
        return 'build';
    }
}

App/Providers/MyMessageServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class MyMessageServiceProvider extends ServiceProvider
{


    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        \App::bind('build', function()
        {
            return new \App\Components\MyMessage;
        });
    }
}

I had registrated the alias and service provider in config/app.php. The problem is that I get an error when I use a facade. Example:

<?php
namespace App\Http\Controllers\Example;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use MyMailer;

class ExampleController extends Controller
{

   public function executeExample(){

      MyMailer::sendMailForAlert('foo@bar.com', 'data', 'new data');


   }


}

I am getting this message: call_user_func_array() expects parameter 1 to be a valid callback, class 'App\Components\MyMessage' does not have a method 'sendMailForAlert'. I don't know why because I am not using 'MyMessage' facade.

Any help is welcome. Thank you



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire