So far i see that there are two file with functions that loaded with composer autoloader.
"autoload": {
"files": [
"src/Illuminate/Foundation/helpers.php",
"src/Illuminate/Support/helpers.php"
],
And then in autoloader:
function composerRequire69685de7f834ebe45f1e02416f8679f0($fileIdentifier, $file)
{
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
require $file;
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
}
}
But actually it just require this file, and all functions comes only to global namespace. Can you explain me why eventually all this functions are accessible from all namespaces?
This is why helpers works in all namespaces:
For functions and constants, PHP will fall back to global functions
or constants if a namespaced function or constant does not exist.
PHP docs
It is because of composers psr-4 autoloading feature.
in the files section of composers autoload you are defining the files it should autoload. So let's consider in one of the files you have function called "calculate". When you now call calculate composer knows in which files it finds the function. If you would remove the files from the autoloading the function call would stop working.
Related
When running composer's update, install, require, dump-autoload, etc.; I suddenly start getting a yellow deprecation notice that says:
Class Foo\Bar\Baz located in ./foo/bar/utility/baz.php does not comply with psr-4 autoloading standard. Skipping.
Before Composer 2.0, one used to get:
Deprecation Notice: Class Foo\Bar\Baz located in ./foo/bar/Baz.php does not comply with psr-4 autoloading standard. It will not autoload anymore in Composer v2.0. in phar:///usr/local/bin/composer/src/Composer/Autoload/ClassMapGenerator.php:201
Why am I getting this notice or warning? What do I need to get rid of it and get ready for Composer 2.0?
This can happen for a variety of reasons.
The important thing is to pay attention to the error message which generally points very accurately to the source of the issue.
Path Case
The most common reason for this is that, as shown in the error message, the case for the different components of the pathname for Bar.php do not match with the case for the fully qualified class name;
foo/bar/Baz.php does not match App\Bar\Baz.
Simply update your application or package so that each path component matches the case of its the namespace it holds:
Foo\Bar\Baz.php
File name and Class name or Namespace differences
Check the pathname against the namespace very carefully. Sometimes your named your class (or your namespace) FooBar, but its path on disk is "foo-bar", for example. Or simply for any reason your namespace does not fully match the pathname of the files.
This will trigger a notice/warning as well. You need to either rename the files or rename the classes (or namespaces).
Usually, changing the path or files is much easier, since changing the class or namespace names would require you refactor code to match the new names, whereas changing the paths will not need you to refactor anything.
Nested namespaces and missing declaration
Let's say that you have:
"autoload": {
"psr-4": {
"Fizz\\Buzz\\": "src/"
}
},
And the class Dummy, defined inside src/Buzz:
// src/Buzz/Dummy.php
namespace Fizz\Buzz
class Dummy {}
The above will work, but will throw the notice like the others. The correct way would be:
// src/Buzz/Dummy.php
namespace Fizz\Buzz\Buzz
class Dummy {}
You'll need not only to make the change on the affected class, but on any other file where this class is used or imported. (e.g. by now declaring use Fizz\Buzz\Buzz\Dummy;).
Helpers:
We add arrayhelper.php to App\Helpers folder
Then we add
"files": [
"app/helpers/arrayhelper.php",
]
to section
"autoload": {
}
in composer.json.
Now it's possible to define methods and use them globally.
Static methods:
make class ArrayHelper
define static method (e.g. doSmthg())
and then it's possible to call it like this ArrayHelper.doSmthg()
Question:
if there is some benefits (e.g. better performance, less memory usage) of using helpers in Laravel or it's just matter of habit?
When to choose between helpers and static methods?
P.S. There was some similar questions, but I read through all answers and I didn't find what I exactly wanted to know.
love the Resources function in Laravel 5.5 which creates at resource prettier for a Model, but I have all my models stored in /App/Models/* instead of directly in the App/*-folder.
This is causing the App/Http/Resources/* not to work.
Results in a "Undefined property: Illuminate\Database\Query\Builder::$map"
This is caused because I stored my Models in a different folder which he can't map to by guessing.
Where and how to define the different placing of the Model?
For what I understand of your problem you have to inject the Model instance in the resource constructor, there is no magical autobinding, i.e.:
use App\Models\User;
use App\Http\Resources\User as UserResource;
Route::get('/user', function () {
return new UserResource(User::find(1));
});
I dont see any problem here, even with namespaced Models.
Not sure of the process you used to move the models to the new Models directory, so check a few things:
Namespace of the models
Each model has:
namespace App\Models;
Check references in these files
app/Http/Controllers/Auth/RegisterController.php
config/auth.php
config/services.php
database/factories/ModelFactory.php
Your Controllers
And change App/ModelExample to App/Models/ModelExample
Update autoload classmap
Add "app/models" to composer.json's classmap autoload section
"autoload": {
"classmap": [
"database",
"app/models"
]
}
Autoload files
Run composer dump-autoload
I am developing my own package in Laravel and I am developing a huge number of helper to use with my package so that it can be used this way:
MyPackage::myfunc1();
MyPackage::myfunc2();
MyPackage::myfunc3();
....
The problem is MyPackage class (MyPackage.php) is becoming huge and the code is becoming very long. This bring hard maintainability to the file.
Is there anyway that I can split the class into a few files for easier maintaining? Or, is there any other way to do so?
Thank you.
As per request:
File: MyPackage/Helpers/FooUtil.php
<?PHP
namespace MyPackage\Helpers;
class FooUtil
{
}
File: MyPackage/Helpers/BarUtil.php
<?PHP
namespace MyPackage\Helpers;
class BarUtil
{
}
Example how to use namespaces to seperate classes and how to use different classes in the same namespace. For more information read:
http://www.php.net/manual/en/language.namespaces.php
Using namespaces in Laravel 4
I generally advice you to read about PSR-0, which is used in Symfony/Laravel to properly support autoloading, composer and packagist: http://petermoulding.com/php/psr
PSR in fact defines how to format namespaces in order to be applicable more globally.
In your example a proper namespacing might be:
Author\MyPackage\Helper\FooClass
I'm new to laravel, coming from Codeigntier. I am trying to get to grips with the model/classes/eloquent convention that Laravel uses. I know that I can make a table in my database called, say, "categories". Then I can make a file in my models folder called category.php containing just the following code:
Class Category extends Eloquent { }
which automatically connects with the table name with the plural version of the name ("categories") and gives me access to all the Eloquent commands in my controller like Category::All();
Here's what I don't get: Do I need to make a new model for every table in my database? In that case I will end up with a bunch of files in my models folder with names like resource1.php, resource2.php, etc, each just containing the same code as above (replacing the name of the class).
Can't I just make one model called, say, "database_handler.php" and then just put all these classes into it in the same place?
Yes, you can create a database_handler.php file and do:
<?php
Class Category extends Eloquent { }
Class Post extends Eloquent { }
Class Client extends Eloquent { }
You can do whatever PHP let's you do, and add many classes to a single .php file is something you can do. But this is not a good practice and Laravel is all about developing application using the best ones.
To load this file automatically, you can do one of many things:
1) Add this file to your app/models folder.
2) Add an entry for it on your composer.json:
"autoload": {
"files": [
"app/database_handler.php"
]
},
And then you'll have to
composer dump-autoload -o
3) Create a different folder for it and also add it to composer json autoload section.
You choose, Laravel will let you free to do whatever you want.