Helper Function In Laravel - composer-php

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';
}
}

Related

Properly Create a Laravel Package

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');
}
}

Class 'GuzzleHttp\Client' not found - local dependencies

I'm writing a library which makes use of GuzzleHttp. The library sits inside of a composer package:
composer.json
"require": {
// ...
"adz/test": "^1.0#dev",
// ...
}
./vendor/adz/test/composer.json
"require": {
"php": "~5.6|~7.0",
"guzzlehttp/guzzle": "^6.3"
},
library controller
namespace adz\test;
use GuzzleHttp\Client;
class User
{
static $client;
public function __construct()
{
self::$client = new \GuzzleHttp\Client();
}
public static function getAll()
{
$res = self::$client->request('GET', 'https://jsonplaceholder.typicode.com/users');
return $res->getBody();
}
}
front controller
use adz\test\User;
class UserController extends Controller
{
var $user, $allUsers;
public function __construct()
{
$this->user = new User();
}
public function getAll()
{
$allUsers = $this->user::getAll();
echo $allUsers;
}
}
At run-time the application reports:
Class 'GuzzleHttp\Client' not found.
If I
composer require guzzlehttp/guzzle
in my front composer.json, then Guzzle works fine. But I don't want to load Guzzle into my front composer.json.
I only want to load Guzzle in my library composer.json file.
What should I do?
Edit: (./vendor/adz/test/composer.json) - full version;
{
"name": "adz/test",
"type": "library",
"description": "desc",
"keywords": [
"adz",
"test"
],
"homepage": "https://github.com/adz/test",
"license": "MIT",
"authors": [
{
"name": "Andy",
"email": "andy#gmail.com",
"homepage": "https://github.com/1cookie",
"role": "Developer"
}
],
"require": {
"php": "~5.6|~7.0",
"guzzlehttp/guzzle": "^6.3"
},
"require-dev": {
"phpunit/phpunit" : ">=5.4.3",
"squizlabs/php_codesniffer": "^3.0"
},
"autoload": {
"psr-4": {
"adz\\test\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"adz\\test\\": "tests"
}
},
"scripts": {
"test": "phpunit",
"check-style": "phpcs src tests",
"fix-style": "phpcbf src tests"
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
}
},
"config": {
"sort-packages": true
}
}

Package does not show on the front-end in Laravel

I try to create a package but for some reason, I cannot access the front-end.
This is my package structure, from the project root directory:
/package/contact/src/routes/web.php
/package/contact/composer.json
/package/contact/ContactServiceProvider
The service provider class looks like the following:
namespace Sidneylab\Contact;
use Illuminate\Support\ServiceProvider;
class ContactServiceProvider extends ServiceProvider
{
public function boot(){
$this->loadRoutesFrom(__DIR__.'/routes/web.php');
}
public function register(){
}
}
Composer.json
"name": "sidneylab/contact",
"description": "This will send email to admin and send contact query to database",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Sidney de Sousa",
"email": "esp.sousa#gmail.com"
}
],
"minimum-stability": "dev",
"require": {},
"autoload": {
"psr-4": {
"Sidneylab\\Contact\\": "src/"
}
},
web.php
Route::get('contact', function(){
return "contact";
});
In my main composer.json file I added this:
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/",
"Sidneylab\\Contact\\": "package/contact/src"
}
},
Also, I imported the service provider inside my confi/app.php
Sidneylab\Contact\ContactServiceProvider::class
Now, when I hit my url/contact it does not return anything. Anything I could have possibly missed?
Anything to do with the things I named?

undefined helper function in laravel phpunit testcase

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

autoload psr-4 gets lost during install

this is the composer.json of my bundle (shortened)
{
"name": "acme/my-bundle",
"type": "library",
"version": "0.5.0",
"autoload": {
"psr-4": {
"Acme\\MyBundle\\": ""
}
}
}
and in my project:
"require": {
"acme/my-bundle": "dev-master"
},
then i run composer install resulting in a installed.json like
[
{
"name": "acme/my-bundle",
"version": "dev-master",
"version_normalized": "9999999-dev",
"type": "library",
"installation-source": "source"
//
// here must be this:
// "autoload": {
// "psr-4": {
// "Acme\\MyBundle\\": ""
// }
// },
// but these lines are missing!
//
}
]
and a autoload-psr4.php:
<?php
// autoload_psr4.php #generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
/* here must be this:
* 'Acme\\MyBundle\\' => array($vendorDir . '/acme/my-bundle'),
* but this line is missing!
*/
);
the autoload is gone, and also other keys like require
what am i missing?
i also tried psr-0, but no success. autoload_namespaces.php is just an empty array.
I did not mention, that I wanted to fetch a package from a private repo! This will make the difference!
So I had to re-specify the autoload
"require": {
"acme/my-bundle": "dev-master"
},
"repositories": [
{
"type": "package",
"package": {
"version": "dev-master",
"name": "acme/my-bundle",
"source": {
"url": "ssh://git#example.com/acme/my-bundle",
"type": "git",
"reference": "test"
},
// THIS IS |
// ADDITIONAL V
"autoload": {
"psr-4": {
"Acme\\MyBundle\\": ""
}
}
}
}
]
see https://stackoverflow.com/a/24193122/816362
Thanks #zacharydanger

Resources