I want to use a helper functions but I got this error in my view :
Call to undefined function createSubCategories()
path of my helper functions:
Http\Controllers\Utilities\Helpers.php
my hlper :
<?php
namespace App\Http\Controllers\Utilities;
function createSubCategories($parent_cat_id = 0)
{
$subs = DB::table('categories')->where('parent_cat_id', '=', $parent_cat_id)->get();
if (count($subs) > 0) {
echo '<ul>';
foreach ($subs as $sub) {
echo '<li>' . $sub->title_fa;
echo $this->createSubCategories(($sub->id));
echo '</li>';
}
echo '</ul>';
}
}
in composer.json :
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
},
"files":
[
"app/Http/Controllers/Utilities/Helpers.php"
]
},
I used composer dump-autoload.
my view:
{{createSubCategories(0)}}
solved:
I just removed the namespace :
namespace App\Http\Controllers\Utilities;
I could reproduce your problem and the solution is to leave out the line
namespace App\Http\Controllers\Utilities;
in your Helpers.php
I have faced same problem that you have mentioned above. in my composer.json, I did not integrate the helper functions. By integrating in composer.json->autoload->files, now it is ok.
"autoload": {
"files":[
"app/Helper/setting.php"
]
}
if your path is alright then it will be ok. For Every change of your helper function you must run the command composer dump-autoload.
Related
I am working on the E-Commerce Application that builds on the laravel platform...
I want to check a stock of every product when I was fetching if stock not available so that will show stock isn't available on product card else show add-to-cart button. The Problem is Product cards available on different pages how can I do this ? can I make helper class function or ... please guide
this is my helper class code,
use App\ProductsAttribute;
function hello($id){
return "hello : ".$id;
}
function getStock($id){
$c = ProductsAttribute::where('id',$id)->select('stock')->get();
$count_stock = json_decode(json_encode($c));
$res = $count_stock;
return $res;
}
or this is my IndexController code :
public function index(Request $request){
// $productsAll = Product::paginate(12);
$productsAll = Product::orderByRaw('RAND()')->take(12)->get();
$arrProducts = Product::inRandomOrder()->skip(0)->take(15)->get();
$arrProducts = json_decode(json_encode($arrProducts));
You can create a app/helpers.php file and reference it inside composer.json
After you did that you have to run the command composer dump-autoload
"autoload": {
"files": [
"app/helpers.php"
],
},
More info about it here: https://laravel-news.com/creating-helpers
Trying to use a non-Laravel package: https://packagist.org/packages/luceos/on-app
Edited composer.json to require it and did the composer install, update, then dump-autoload -o.
This package requires an initialization: vendor/luceos/on-app/src/OnAppInit.php
Which isn't a class and only has the one method. But it doesn't seem to be loaded when I try to bind it in a service provider. The version for the cloud is initiated in the OnAppInit.php but that isn't being done so the "version isn't supported" error comes up of course.
I know that I am missing a small detail but can't find it. Maybe in the service provider??
composer.json
"require": {
"luceos/on-app": "~3.5"
"autoload": {
"psr-4": {
"Luceos\\OnApp\\": "vendor/luceos/on-app/src/"
config/app.php
'providers' => [
'App\Providers\OnAppServiceProvider',
app/Providers/OnAppServiceProvider.php
public function register()
{
$this->app->bind('onapp', function($app)
{
$hostname = 'http://cloud';
$username = 'email#foo.com';
$password = 'api_key';
$factory = new \OnApp_Factory($hostname, $username, $password);
$setting = $factory->factory('Settings')->getList();
return $setting;
});
}
Looks like its there...
vendor/composer/autoload_files.php
$vendorDir . '/luceos/on-app/src/OnAppInit.php',
vendor/composer/autoload_psr4.php
'Luceos\\OnApp\\' => array($vendorDir . '/luceos/on-app/src'),
Regarding the Guzzle question:
Just include it in your composer.json file:
"guzzlehttp/guzzle": "~5.0"
And then just use the normal
$client = new GuzzleHttp\Client();
Just don't forget to to composer dump-autoload
I have the following directory structure
app
| ...
| lib
| | Macro.php
| | Events.php
| ...
In my composer.json I have
"autoload": {
"classmap": {
...
"app/lib"
...
}
}
Events.php loads fine and updates the DB when the event triggers:
<?php
Event::listen('auth.login', function($user) {
$user->last_login = new DateTime;
$user->save();
});
Macros.php will always return the error Method messageBox does not exist if the macro is used in a view:
<?php
Form::macro('test', function()
{
return 'test';
});
I have run composer dump-autoload. Why is the Macros.php not autoloading?
The autoload classmap section in composer.json maps excatly that: classes. So it won't help you in this instance, because you don't have a class declaration in that file. You can however autoload files as well with composer, by adding this to the autoload section:
"autoload": {
"classmap": [
...
],
"files": [
"app/lib/Macros.php"
]
}
As an alternative you could always manually include the file in Laravel's app/start/global.php like so:
require app_path() . '/lib/Macros.php';
I am new to namespaces. Using laravel.
my folder structure is
app
-lib
-Services
-Users
-UserCreator.php
-controllers
-SiteController.php
composer.json
My problem is it says Class Services/Users/UserCreator does not exist everytime I try and load it.
UserCreator.php:
<?php
namespace Services\Users;
class UserCreator {
}
SiteController.php
<?php
use Services\Users\UserCreator;
// more code here...
if ($validator->fails()) {
echo 'fail';
} else {
$userCreator = App::make('Services/Users/UserCreator');
if ($userCreator::create(Input::all())) {
return Redirect::to('/')->with('message', 'Account Created');
} else {
return Redirect::to('/error');
}
}
}
Composer.json
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
"psr-4": {
"Services\\Users\\": "app/lib/Services/Users/UserCreator"
}
}
I only included what I thought you would need. Let me know if you need anything else. Thanks!
In your composer.json change psr-4 section to this
"psr-4": {
"Services\\": "app/lib/Services"
}
Also change
$userCreator = App::make('Services/Users/UserCreator');
to
$userCreator = App::make('Services\Users\UserCreator');
^ ^
How can I autoload helper functions (outside of any class)? Can I specify in composer.json some kind of bootstrap file that should be loaded first?
You can autoload specific files by editing your composer.json file like this:
"autoload": {
"files": ["src/helpers.php"]
}
(thanks Kint)
After some tests, I have came to the conclusions that adding a namespace to a file that contains functions, and setting up composer to autoload this file seems to not load this function across all the files that require the autoload path.
To synthesize, this will autoload your function everywhere:
composer.json
"autoload": {
"files": [
"src/greetings.php"
]
}
src/greetings.php
<?php
if( ! function_exists('greetings') ) {
function greetings(string $firstname): string {
return "Howdy $firstname!";
}
}
?>
...
But this will not load your function in every require of autoload:
composer.json
"autoload": {
"files": [
"src/greetings.php"
]
}
src/greetings.php
<?php
namespace You;
if( ! function_exists('greetings') ) {
function greetings(string $firstname): string {
return "Howdy $firstname!";
}
}
?>
And you would call your function using use function ...; like following:
example/example-1.php
<?php
require( __DIR__ . '/../vendor/autoload.php' );
use function You\greetings;
greetings('Mark'); // "Howdy Mark!"
?>