Asked  7 Months ago    Answers:  5   Viewed   36 times

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.

 Answers

72

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

Take the following example:

app/models/File.php

namespace AppModels;

class File {

    public function someMethodThatGetsFiles()
    {

    }
}

app/controllers/FileController.php

namespace AppControllers;

use AppModelsFile;

class FileController {

    public function someMethod()
    {

        $file = new File();
    }
}

Declare the Namespace:

namespace AppControllers;

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 AppModelsFile;

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

Alternatively you can just call:

$file = new AppModelsFile();

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

Wednesday, March 31, 2021
 
edorian
answered 7 Months ago
61

Putting a

<?php xdebug_break(); ?>

into your blade file works pretty well. Even in my tests, PHPstorm jumps to the next PHP statement in some cases.

Why this works:

Laravel processes the blade file to a normal PHP file in the cache folder. But the PHP statement xdebug_break(); will be transferred there and cause the program to halt at the position you want it to (in the cache file).

Wednesday, March 31, 2021
 
employeegts
answered 7 Months ago
31

I guess you try to show the search results after searching. The problem is this line.

return Redirect::to('property/search', compact('properties'));

After you get the search result you should call a view, not redirect.

return view('property.search', compact('properties'));

But make sure you have the view file.

Source

Wednesday, March 31, 2021
 
cyber_truite
answered 7 Months ago
30

Laravel 4.x doesn't even support ENV files. You just have to see whether settings in ./config/[env name]/database.php are correct.

Saturday, July 31, 2021
 
Sugrue
answered 3 Months ago
70

Found out a way to get this working here:

$result = DB::select('call getLibraryList(?)',array($email));
Friday, August 13, 2021
 
Indranil
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :