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

kjones
  • 1,339
  • 1
  • 13
  • 28
Dariux
  • 3,953
  • 9
  • 43
  • 69

2 Answers2

4

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{}
Shhetri
  • 152
  • 7
  • 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? – Dariux Dec 09 '14 at 12:16
  • 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. – Shhetri Dec 09 '14 at 12:27
  • @Darius.V though the solution works I know that I am not being much of a help here. Take a look at this, [namespaces](http://daylerees.com/codebright/the-primers). You will be clear about the namespaces. – Shhetri Dec 09 '14 at 12:38
  • 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 http://stackoverflow.com/questions/14714848/using-namespaces-in-laravel-4 - which suggests namespacing starting from App folder namespace App\Models; So I guess this should be better? – Dariux Jan 08 '15 at 15:08
  • 1
    yeah...you can start with App. while namespacing a file, use the folder structure convention. It will be easy to give the namespace. – Shhetri Jan 09 '15 at 01:14
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.

Community
  • 1
  • 1
Dariux
  • 3,953
  • 9
  • 43
  • 69