0

I have a class called Messaging and I created a facade to using it like

Messaging::getConversationMessages($conv_id, $user_id);

I have followed all the instructions in this link below

How do I create a facade class with Laravel?

This is my MessagingServiceProvider calss below which does the binding

<?php
use Illuminate\Support\ServiceProvider;

class MessagingServiceProvider extends ServiceProvider {
/**
 * Register the service provider.
 *
 * @return void
 */
public function register() {
    App::bind('messaging', function()
    {
        return new \Messaging\Messaging;
    }
    );
}

}

Below is my facade class that I created for me to use it in the way I wanted to

<?php 

use Illuminate\Support\Facades\Facade;

class Messaging extends Facade
{

/**
 * Get the registered name of the component.
 *
 * @return string
 */
protected static function getFacadeAccessor() { return 'messaging'; }

}

I have placed my MessagingServiceProvider.php inside a folder called serviceproviders inside app folder, and placed the messaging.php(the file containing the facade class) inside a folder called facade in the app folder and added them to auto load.

Below is the model class for the facade

<?php
namespace Messaging;

use Eloquent; // if you're extending Eloquent

class Messaging extends Eloquent {
    ...
}

After doing all this still I am getting an error "Non-static method Messaging\Messaging::getConversationMessages() should not be called statically, assuming $this from incompatible context"

Community
  • 1
  • 1
karthi
  • 5
  • 4

1 Answers1

0

You are not using your Facade. Try to namespace it and use a different name:

<?php namespace Messaging;

use Illuminate\Support\Facades\Facade;

class MessagingFacade extends Facade
{
   ...
}

Then you

composer dumpautoload

And try to use it this way:

Messaging\MessagingFacade::getConversationMessages()

If it works for you, you create an alias for it in app/config/app.php.

'Messaging' => 'Messaging\MessagingFacade',

And you should be able to use it as:

Messaging::getConversationMessages()

About namespaced classes, every time you need to a class from another namespace, you need to go root:

\DB::whatever();
Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
  • I think it works fine as now I have a different error "Class 'Messaging\DB' not found" and the code is as below, but wondering is this because of using the namespace as Messaging??/ – karthi Apr 02 '14 at 15:04
  • sorry I am completely new to this ... any help would be great – karthi Apr 02 '14 at 15:06
  • Yeah, looks like it's working you just have to tell PHP that DB is in another namespace using `\DB::method()` – Antonio Carlos Ribeiro Apr 02 '14 at 15:20