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

∥☆過路亽.° 提交于 2019-12-11 07:21:29

问题


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


回答1:


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{}



回答2:


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.



来源:https://stackoverflow.com/questions/27374347/phpcs-class-must-be-in-a-namespace-of-at-least-one-level-how-to-fix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!