I've a helper file helper.php where I keep some helper functions.
//helper.php
function isAuthLiked($authLikedPosts, $post)
{
return !! Auth::check() && $authLikedPosts->contains('id', $post->id);
}
Now in my test case, I wrote:
$this->assertTrue(isAuthLiked($authrenominations, $post[0]));
When I ran the test case, I get the error:
Fatal error: Call to undefined function isAuthLiked() in
C:\wamp\www\Nom7\tests\integration\UserTest.php on line 304
I've added the helper file in the compose.json auto-load. But the problem persists.
"autoload": {
"classmap": [
"database",
"app/Http/Controllers",
"app/Models"
],
"files":[
"app/helper.php"
],
"psr-4": {
"App\\": "app/",
"Acme\\": "app/Acme/"
}
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
],
"files":[
"app/helper.php"
]
},
Check your path. You are using relative paths in your files array. You are running your tests from your tests directory, so cannot find app/helper.php
Related
Hello Guys I tried To Made A Helper Function Like asset Using Laravel 9.21.6
This Is My Code
Helper.php
<?php
function show_name() {
return 'Ahmed Emmam';
}
routes.php
Route::get('test-helper' , function () {
show_name();
});
composer.json
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
"files" : [
"app/Helpers/Helper.php"
],
The Error Is Call to undefined function show_name()
i ran Composer dump-autoload but it's failed
Can Anyone Help Me Please
You should load the other files within the autoload field.
{
"autoload": {
"psr-4": { ... },
"files": [
"app/Helpers/Helper.php"
]
}
}
https://getcomposer.org/doc/04-schema.md#files
Try to usefunction_exists function
if (!function_exists('show_name')) {
function show_name()
{
return 'Ahmed Emmam';
}
}
Composer.json
"autoload": {
"exclude-from-classmap": [
"vendor\\laravel\\passport\\src\\Http\\Controllers\\AccessTokenController.php"
],
"psr-4": {
"App\\": "app/",
"Laravel\\Passport\\": "app/Override/" //tried "Passport//" only also
}
},
I have file AccessTokenController.php which lie in vendor\laravel\passport\src\Http\Controllers\ and i create Override folder inside App directory on where i copied that file and make changes in code
And finally when i do :
composer dump-autoload
i get error as :Class Laravel\Passport\Http\Controllers\AccessTokenController located in ./app/Override/AccessTokenController.php does not comply with psr-4 autoloading standard. Skipping.
seems problem with namespace which i couldn't figure out though trying some of the ways ..
Anyone can help me ?
I would simply override the route from within the app as mentioned here. No need for the approach you mentioned above.
However, if you want to keep going this route you can try the following.
"psr-4": {
"App\\": "app/",
}
"exclude-from-classmap": [
"Laravel\\Passport\\Http\\Controllers\\AccessTokenController"
],
"files": [
"app/Override/AccessTokenController.php"
],
I solved it by including complete namespace of AccessTokenController.php.so final code would look like this:
"autoload": {
"exclude-from-classmap": [
"vendor\\laravel\\passport\\src\\Http\\Controllers\\AccessTokenController.php"
],
"psr-4": {
"App\\": "app/",
"Laravel\\Passport\\Http\\Controllers\\":"app/Override/"
}
}
I am having some issues with developing a package. I have read through several tutorials on the web and followed their advice but for some reason I still run into the same issue.
What I am trying to do: Develop a package in an active project
In my Laravel folder, I have a packages/companyname/laravel-model-notes. Inside that package, I have a composer.json file with the following:
{
"name": "companyname/laravel-model-notes",
"description": "A package to add notes to an eloquent model in Laravel.",
"type": "library",
"require": {},
"config": {
"sort-packages": true
},
"autoload": {
"psr-4": {
"CompanyName\\LaravelModelNotes\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"CompanyName\\LaravelModelNotes\\Tests\\": "tests"
}
},
"extra": {
"laravel": {
"providers": [
"CompanyName\\LaravelModelNotes\\NoteServiceProvider"
]
}
}
}
And then in my laravel composer.json file, I have added the following:
"autoload": {
"psr-4": {
"App\\": "app/",
"CompanyName\\LaravelModelNotes\\": "packages/companyname/laravel-model-notes/src"
},
"classmap": [
"database/seeds",
"database/factories"
],
"files": [
"app/Helpers/ThemeHelper.php",
"app/Helpers/BaseHelper.php"
]
},
When I manually add the service provider to the App.php things work, however artisan commands like publishing the migrations does not work. For some reason, Laravel does not see the package?
Is there some special way of building a package inside of a project and having that project recognize the package?
I am using Laravel 7.
Below is my service provider:
class NoteServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*/
public function boot()
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../config/config.php' => config_path('notes.php'),
], 'config');
if (! class_exists('CreateNotesTable')) {
// TODO: If tenant folder exists publish there too.
$this->publishes([
__DIR__.'/../database/migrations/create_notes_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_notes_table.php'),
], 'migrations');
}
}
}
/**
* Register the application services.
*/
public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'notes');
}
}
In my composer.json I have written:
"autoload": {
"psr-4": {
"Pmochine\\MyOwnPackage\\": "src/"
},
"files": [
"src/helpers.php"
]
},
But somehow even after composer dump-autoload the functions are not loaded. I get "Call to undefined function". To create the package I used a package generator. Maybe it has something to do that it creates a symlink in the vendor folder?
Inside helpers I have written
<?php
if (! function_exists('myowntest')) {
function myowntest()
{
return 'test';
}
}
In the package service provider, try adding this:
// if 'src/helpers.php' does not work, try with 'helpers.php'
if (file_exists($file = app_path('src/helpers.php'))) {
require $file;
}
What you are doing is best practise and should work. I took the composer.json from barryvdh/laravel-debugbar as an example. https://github.com/barryvdh/laravel-debugbar/blob/master/composer.json
{
"name": "barryvdh/laravel-debugbar",
"description": "PHP Debugbar integration for Laravel",
"keywords": ["laravel", "debugbar", "profiler", "debug", "webprofiler"],
"license": "MIT",
"authors": [
{
"name": "Barry vd. Heuvel",
"email": "barryvdh#gmail.com"
}
],
"require": {
"php": ">=5.5.9",
"illuminate/support": "5.1.*|5.2.*|5.3.*|5.4.*|5.5.*",
"symfony/finder": "~2.7|~3.0",
"maximebf/debugbar": "~1.13.0"
},
"autoload": {
"psr-4": {
"Barryvdh\\Debugbar\\": "src/"
},
"files": [
"src/helpers.php"
]
}
}
My guess is that you are not requiring your own package the correct way in the main composer.json?
Just need to call in your main project
composer update you/your-package
Source
The only thing that has worked for me is to run composer remove <vendor>/<package> and require it back again. The files section was ignored otherwise.
This seems to happen while developing locally the package and making changes on the composer.json file.
I have this composer config:
under classmap: "app/models"
under psr-4: "App\\Models\\": "app/models"
"autoload": {
"classmap": [
"database",
"app/models"
],
"psr-4": {
"App\\": "app/",
"App\\Models\\": "app/models"
}
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
]
},
Here is my model:
namespace App\Models;
class TemplateRow extends Model{
protected $table = "template_rows";
}
in my controller i did: $row = new TemplateRow(); and I got class not found exception.
I did dump-autoload.
Thanks
You have to actually reference the model with it's namespace. You can either write:
$row = new \App\Models\TemplateRow();
Or add this before the class instead:
use App\Models\TemplateRow;
Also note that you shouldn't even have to add the entry under psr-4. If you're directory structure follows the namespacing. To be certain, call your folder Models and not models