phpcs - class must be in a namespace of at least one level - how to fix?

13.9k views Asked by At

I am using laravel 4.2.

Lets say there is such class:

class BShopsController extends ShopsController

To fix this, I try to use name space lets say this:

namespace app\controllers;

and then it does not find ShopsController

so I add

use \ShopsController;

Then I get error:

Class BShopsController does not exist

What namespace should I use first of all so it would not break anything?

Edit:

BShopsController and ShopsController are in folder Shops

2

There are 2 answers

0
Dariux On BEST ANSWER

So with the help of Shhetri and this Using namespaces in Laravel 4

I did this way:

namespace App\Controllers\Shops;

class BShopsController extends ShopsController{}

Also in routes.php then need to change to this:

Route::controller('shops', 'App\Controllers\Shops\ShopsController');

And where calling action() method - also need to use namespace.

Also needed to run

composer dump-autoload -o 

otherwise were errors.

Also in ShopsContrller needed to to this:

use \App\Controllers\BaseController;

Because Shops controller was in another namespace than BaseController and cannot find it. But is extending from BaseController, so need it.

5
Shhetri On

As your files are inside the Shops folder and I believe that the Shops folder is inside the app folder you should namespace your class the following way.

<?php namespace Shops;

class BShopsController extends ShopsController{}

Similarly,

<?php namespace Shops;

    class ShopsController{}