Using namespaces in Laravel 4

I am new to Laravel and use PHP namespaces in general. I had no problems until I decided to create a model called "File". How can I use the namespace correctly to use the File model class?

Files app/controllers/FilesController.php and app/models/File.php . I am trying to create a new File in FilesController.php .

+44
php namespaces laravel laravel-4
Feb 05 '13 at 18:58
source share
5 answers

Putting names is pretty simple once you get it.

Take the following example:

application / models / file.php

 namespace App\Models; class File { public function someMethodThatGetsFiles() { } } 

application / controllers / FileController.php

 namespace App\Controllers; use App\Models\File; class FileController { public function someMethod() { $file = new File(); } } 

Declare a namespace:

 namespace App\Controllers; 

Remember, once you have placed a class in a namespace to access any of the PHP built-in classes, you need to call them from the root namespace. for example: $stdClass = new stdClass(); will become $stdClass = new \stdClass(); (see \ )

Import other namespaces:

 use App\Models\File; 

This allows you to use the File class without a namespace prefix.

Alternatively, you can simply call:

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

But it’s best to put it at the beginning in the use statement, since then you can see all the dependencies between the files without looking at the code.

After that, you need to run composer dump-autoload to update the Composer autoload function to take into account your newly added classes.

Remember that if you want to access the FileController via a URL, you need to define a route and specify the full namespace as follows:

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

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

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

+89
Feb 06 '13 at 22:01
source share

load your class first 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 Document

+5
Feb 05 '13 at 23:01
source share

To space your model at the top of your model class immediately after opening

Then, when you call from the controllers, you call new What whatever \ Model;

You may have to do a dump at first with the composer.

+2
Feb 06 '13 at 12:37
source share

look at it .. hope will clear your request ....

 <?php namespace app\controllers; use yii\web\Controller; use app\models\users; class UserController extends Controller{ public function actionIndex() { echo "working on ....."; } } 
0
Aug 28 '15 at 6:58
source share

Namespaces are defined at the top of the PHP classes immediately after the opening php script tag as follows:

  <?php namespace MyNameSpace; 

When you want to use a namespace class in some other class, you define it as follows:

 new MyNameSpace\PhpClass; 

or import it at the top of the file (after namespaces, if any) as follows:

  <?php //namespace use MyNameSpace\MyPHPClass; //then later on the code you can instantiate the class normally $myphpclass = new MyPHPClass(); 

In Laravel namespaces, you can define wherever the composer can automatically load them, I would recommend defining namespaces in the application directory. Thus, you can define a namespace, such as Utils, for storing Utility classes by creating the Utils directory in the application directory, creating our utility classes and defining the namespace, as we did above.

After that, you ran a command to ask the composer to automatically load the classes:

  $ composer dump-autoload 
0
Aug 21 '19 at 0:01
source share



All Articles