45

I'm new to Laravel and using PHP namespaces in general. I didn't run into any problems until I decided to make a model named File. How would I go about namespacing correctly so I can use my File model class?

The files are app/controllers/FilesController.php and app/models/File.php. I am trying to make a new File in FilesController.php.

Laurence
  • 58,936
  • 21
  • 171
  • 212
user2030045
  • 455
  • 1
  • 5
  • 5

5 Answers5

92

Namespacing is pretty easy once you get that hang of it.

Take the following example:

app/models/File.php

namespace App\Models;

class File {

    public function someMethodThatGetsFiles()
    {

    }
}

app/controllers/FileController.php

namespace App\Controllers;

use App\Models\File;

class FileController {

    public function someMethod()
    {

        $file = new File();
    }
}

Declare the Namespace:

namespace App\Controllers;

Remember, once you've put a class in a Namespace to access any of PHP's built in classes you need to call them from the Root Namespace. e.g: $stdClass = new stdClass(); will become $stdClass = new \stdClass(); (see the \)

"Import" other Namespaces:

use App\Models\File;

This Allows you to then use the File class without the Namespace prefix.

Alternatively you can just call:

$file = new App\Models\File();

But it's best practice to put it at the top in a use statement as you can then see all the file's dependencies without having to scan the code.

Once that's done you need to them run composer dump-autoload to update Composer's autoload function to take into account your newly added Classes.

Remember, if you want to access the FileController via a URL then you'll need to define a route and specify the full namespace like so:

Route::get('file', 'App\\Controllers\\FileController@someMethod');

Which will direct all GET /file requests to the controller's someMethod()

Take a look at the PHP documentation on Namespaces and Nettut's is always a good resource with this article

Josh Holloway
  • 1,683
  • 2
  • 13
  • 14
  • 1
    @JoshHollowway if having FileController extend BaseController this would fail with error of BaseController not found, how do you get around this? – AndHeiberg Feb 13 '13 at 12:55
  • 1
    You'll need to reference BaseController from the root namespace with a \ like this: \BaseController – Josh Holloway Feb 14 '13 at 20:37
  • 3
    i follow the above procedure, but in the end i get the error: Class 'App\Controllers\BaseController' not found . after making \BaseController i get the error: Class FileController does not exist . i also do composer dump-autoload – Dexture Feb 27 '14 at 16:36
  • @JoshHolloway Any chance for an example that uses extends? – AturSams Sep 09 '14 at 08:49
  • @Zehelvion - http://stackoverflow.com/questions/27374347/phpcs-class-must-be-in-a-namespace-of-at-least-one-level-how-to-fix/27855553#27855553 – Dariux Jan 09 '15 at 07:46
  • @JoshHolloway - but now the problem I see of lot code repetition. In every controller I need to call use App\Models\X.php to use it. Any smarter way to avoid this? – Dariux Jan 09 '15 at 07:47
  • What actually is namespace use for? – Angger Apr 30 '17 at 06:14
4

first, load your class with:

$ composer dump-autoload

then

$file = new File;

// your stuff like:
$file->name = 'thename';
$file->active = true;

$file->save();

Section: Insert, Update, Delete on Laravel 4 Eloquent's doc

Pierre Broucz
  • 687
  • 5
  • 8
2

To namespace your model, at the top of your model class right after the opening

Then when you call from controllers you will call new Whatever\Model;

You probably have to do a dump-autoload with composer the first time around.

0

have a look to it.. hopefully will clear your query....

<?php

 namespace app\controllers;
 use yii\web\Controller;
 use app\models\users;
  class UserController extends Controller{
 public function actionIndex()
 {
echo "working on .....";
}
}
Muhammad Saeed
  • 81
  • 3
  • 10
0

Namespaces are defined at the top of PHP classes right after the opening php script tag like this:

 <?php
   namespace MyNameSpace;

When you then want to use the namespaced class in some other class, you define it like this:

new MyNameSpace\PhpClass;

or import it at the top of the file (after namespaces if present) like this:

 <?php

   //namespace

   use MyNameSpace\MyPHPClass;

   //then later on the code you can instantiate the class normally
   $myphpclass = new MyPHPClass();

In Laravel namespaces can be defined anywhere composer can autoload them, I'd recommend defining namespaces within the app directory. So you can define a namespace like Utils for holding Utility classes by creating a Utils directory in the app directory, creating our utility classes and defining the namespace as we did above.

Afterwards you have run the command to ask composer to autoload classes:

 $ composer dump-autoload
Eric McWinNEr
  • 534
  • 6
  • 19