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

2

2

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

Goatsbeard answered 9/12, 2014 at 8:27 Comment(0)
G
3

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.

Goatsbeard answered 9/1, 2015 at 7:10 Comment(0)
L
5

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{}
Layout answered 9/12, 2014 at 12:12 Comment(5)
Shops folder is in controllers folder. Did not mention this because files were controllers, so I assumed you will know already. So now how that change this? Namespace should be controllers/Shops ? Why not app/controllers/Shops for example?Goatsbeard
Sorry, my bad. I mixed the controllers with the repository thing. Anyways, the solution is still the same. Use the namespace of Shops for the files inside the Shops folder.Layout
@Darius.V though the solution works I know that I am not being much of a help here. Take a look at this, namespaces. You will be clear about the namespaces.Layout
that worked. But then I came to file which is in controllers folder. Using namespace just a backslash is not possible. And then found another question #14715348 - which suggests namespacing starting from App folder namespace App\Models; So I guess this should be better?Goatsbeard
yeah...you can start with App. while namespacing a file, use the folder structure convention. It will be easy to give the namespace.Layout
G
3

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.

Goatsbeard answered 9/1, 2015 at 7:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.