Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using namespaces in Laravel 4

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.

like image 742
user2030045 Avatar asked Feb 05 '13 18:02

user2030045


People also ask

How to create a custom namespace in Laravel 8?

To create a custom namespace in Laravel 8, create a separate controller with forward-slash (/) using the following command. php artisan make:controller Admin/UserController --resource --model =User If you closely look at the above command, you can see that we have used the forward slash to separate our namespace Admin.

What is the use of use keyword in Laravel?

The use keyword allows the developers to shorten the namespace. The default namespace used in Laravel is App, however a user can change the namespace to match with web application. Creating user defined namespace with artisan command is mentioned as follows −

What are namespaces in Java?

Namespaces can be defined as a class of elements in which each element has a unique name to that associated class. It may be shared with elements in other classes. The use keyword allows the developers to shorten the namespace.


1 Answers

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

like image 101
Josh Holloway Avatar answered Sep 23 '22 13:09

Josh Holloway