0

In Laravel 5 I try to add MyClass into IoC Container.

So I add the follow structure:

app/Libs/MyClass.php

<?php namespace App\Libs;

use App\Interfaces\MyClassInterface;

class MyClass implements MyClassInterface {

    public function list($itemId)
    {
        // do any things
    }

}

app/Facades/MyClassFacade.php

<?php namespace App\Facades;

use Illuminate\Support\Facades\Facade;

class MyClassFacade extends Facade {

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

app/Providers/MyClassServiceProvider.php

<?php namespace App\Providers;

use Illuminate\Support\ServiceProvider;

use App\Libs\MyClass;
class MyClassServiceProvider extends ServiceProvider {
    public function boot()
    {
        //
    }
    public function register()
    {
        $this->app->bind('myclass', function () {
            return new MyClass();
        });
    }
}

app/config/app.php (in providers)

'App\Providers\MyClassServiceProvider',

app/config/app.php (in aliases)

'MyClass' => 'App\Facades\MyClassFacade',

app/Http/Controllers/MyClassController.php

<?php namespace App\Http\Controllers;

class MyClassController {

public function index($itemId = null)
{
    $itemList = MyClass::list($itemId); // this is line error

    return view('item.list')->with($itemList);
}

app/Http/routes.php

Route::pattern('numeric', '[0-9]+');
Route::get('item/{numeric?}', array('as' => 'item_list', 'uses' => 'MyClassController@index'));

Then I ran the command

composer update

and

composer dump-autoload

But aways I get this Error:

Class 'App\Http\Controllers\MyClass' not found

Where am I going wrong?

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
JulianoMartins
  • 543
  • 1
  • 8
  • 19

1 Answers1

1

In the line with error you need to use:

$itemList = \MyClass::list($itemId); // this is line error

instead of:

$itemList = MyClass::list($itemId); // this is line error

or change this file into:

<?php namespace App\Http\Controllers;

use MyClass;

class MyClassController {

public function index($itemId = null)
{
    $itemList = MyClass::list($itemId); // this is line error

    return view('item.list')->with($itemList);
}

This is not Laravel specific issue. This is how you use namespaces. More at How to use objects from other namespaces and how to import namespaces in PHP

Community
  • 1
  • 1
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • I can not do that Marcin, because that way you told MyClass class should be a static class. I know since there is an alias for the object, the container would take care of the rest. – JulianoMartins Oct 06 '14 at 19:17
  • @JulianoMartins Not really. If you use here `MyClass::` you tell PHP that you want to use MyClass from current namespace. The only way to solve it is either to add leading backslash to tell that this class in global namespace (as you defined your alias) or use namespace importing with `use`. There is no other way if Controller is not in global namespace. – Marcin Nabiałek Oct 06 '14 at 19:19
  • Marcin Nabiałek, you're right. Now it worked great! Thank you! – JulianoMartins Oct 06 '14 at 19:25