Laravel queue job does not load file in - laravel

I am a bit stuck with loading in a file for Laravel Queue/Job
I am using Laravel queued/async jobs (lets call it job for easy use)
Oke lets start from the beginning, we have our own translation function and we also named it __() just like the default from Laravel, don't ask me why etc. (easy solution is to just rename it, I know) but this is what I have to stick with (unless this is unfixable maybe).
So to declare the function before Laravel does we insert the function just before the autoload in the index.php like this.
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels great to relax.
|
*/
require __DIR__.'/../app/Helpers/localization.php'; <-- yes this file
require __DIR__.'/../vendor/autoload.php';
And this works fine for the website and if we execute jobs with SomeJob::dispatchNow() (not async)
But when we want to do a async job like SomeJob::dispatch() the index.php is not called so the file is never required so neither is the function. (or am I wrong?)
I tried it with composer.js autoload
"autoload": {
"psr-4": {
"App\\": "app/"
},
"classmap": [
"database/seeds",
"database/factories"
],
"files": [
"app/Helpers/helpers.php", <-- works fine
"app/Helpers/Localization.php" <-- it does include tho
]
},
now in the file we use the if (! function_exists('__')) but at that point the function is already declared, on the website itself it doesn't work either.
So short saying, require at the index.php only works from the website directly and not from async jobs cause index.php is never called when a queue execute the job.
Using the composer autoload doesn't work for both the website or the job cause the function is already declared by Laravel before we declare it.
So where should I require the file/declare the function so that both the website directly and the async job can use our version of the function.
P.S. I know my english isn't that great, so if anything is unclear or even if I miss any information, please ask me and I try to edit the post to be more clear.

You can add your function definition to the start of the bootstrap file of laravel bootstrap/app.php
Or (since i suspect SomeJob::dispatch() to use artisan), you can require your file in index.php and in artisan
#!/usr/bin/env php
<?php
define('LARAVEL_START', microtime(true));
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader
| for our application. We just need to utilize it! We'll require it
| into the script here so that we do not have to worry about the
| loading of any our classes "manually". Feels great to relax.
|
*/
require __DIR__.'/app/Helpers/localization.php';
require __DIR__.'/vendor/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';

Related

Executing config:cache command during deployment

I have a secret test key for a payment gateway in the .env file.
APP_TIMEZONE = 'Africa/Lagos'
APP_PAYSTACK_KEY = sk_test_b6c0b4925403blablabla
Reason is that other people working on the project can use their own test key if they have. So in a payment controller i get the value of this key like so:
"authorization: Bearer " .env('APP_PAYSTACK_KEY' , 'sk_test_b6c0b4925403blablabla')
During deployment I intend running config:cache so that Laravel won't be going a long trip in getting the required configuration setups. But from Laravel documentation :
If you execute the config:cache command during your deployment
process, you should be sure that you are only calling the env
function from within your configuration files. Once the configuration
has been cached, the .env file will not be loaded and all calls to
the env function will return null.
So my question is how can I set this APP_PAYSTACK_KEY in the config file and how to retrieve it anywhere in my app?
You can add paystack to your config/services.php file:
<?php
return [
/*
|--------------------------------------------------------------------------
| Third Party Services
|--------------------------------------------------------------------------
|
| This file is for storing the credentials for third party services such
| as Stripe, Mailgun, SparkPost and others. This file provides a sane
| default location for this type of information, allowing packages
| to have a conventional place to find your various credentials.
|
*/
// ...
'paystack' => [
'key' => env('APP_PAYSTACK_KEY', 'sk_test_b6c0b4925403blablabla'),
],
];
Then call config() helper method instead of env() on your controller:
"authorization: Bearer " .config('services.paystack.key')
Now you're safe to cache your configs via calling php artisan config:cache.

How to create custom helper file in laravel 5.6?

When I create an autoload helper file with composer following error ocures:
Fatal error: composerRequireebefe31fc60fbe2897fba8c156ec310c(): Failed
opening required
'D:\xampp\htdocs\common_module\vendor\composer/../../app/Http/helpers.php'
(include_path='D:\xampp\php\PEAR') in
D:\xampp\htdocs\common_module\vendor\composer\autoload_real.php on
line 66
Following are steps to create a Custom Helper In Laravel 5.5
Step : 1 Create app/helpers.php file
First, create one helper class file in app/helpers.php path and in this file we are write our any custom helper logic into the function.
Step : 2 Add app/helpers.php file in composer.json file
Now, we are add our app/helpers.php file in composer.json file for a autoload section.
"autoload": {
"classmap": [
...
],
"psr-4": {
"App\\": "app/"
},
"files": [
"app/helpers.php" //Add This Line
]
},
After done this then once we are run following command.
composer dump-autoload
for more visit following link
Visit How To Create Custom Helper In Laravel 5.5
I hope this will help you...
In older version of Laravel I do the following:
app folder create a Helpers folder
Create a class YourHelper class file
in that class file set namespace to App\Helpers
Name the class file
in config/app.php in aliases create an alias (so you can call the helper in your view also) 'YourHelper'=>'App\Helpers\YourHelper::class'
If you are new to Laravel or PHP, let’s walk through how you might go about creating your own helper functions that automatically get loaded by Laravel.
Creating a Helpers file in a Laravel App
you can organize the location of your helper file(s) however you want, however, here are a few suggested locations:
app/helpers.php
app/Http/helpers.php
I prefer to keep mine in app/helpers.php in the root of the application namespace.
Autoloading
PHP functions cannot be autoloaded. However, we have a much better solution through Composer than using require or require_once.
So composer has a files key (which is an array of file paths) that you can define inside of autoload:
"autoload": {
"files": [
"app/helpers.php"
],
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
}
},
Once you add a new path to the files array, you need to dump the autoloader:
composer dump-autoload
(Use this command after changing composer.json.)
Now on every request the helpers.php file will be loaded automatically because Laravel requires Composer’s autoloader in public/index.php:
require __DIR__.'/../vendor/autoload.php';
Defining Functions
Defining functions in your helpers class is the easy part, although, there are a few caveats. All of the Laravel helper files are wrapped in a check to avoid function definition collisions:
if (! function_exists('env')) {
function env($key, $default = null) {
// ...
}
}
I prefer to use function_exists checks in my application helpers, but if you are defining helpers within the context of your application, you could forgo the function_exists check.

Laravel5.5 after use php artisan app:name Class not found

I am new developer on Laravel, now I'm using Laravel version 5.5
I got the problem after used php artisan app:name on my project, I got the problem:
In ProviderRepository.php line 208: Class
'App\Providers\AppServiceProvider' not found
as the captured image below:
As this error, I can not use php artisan commands or composer commands anymore can you guys please help me to solve this problem I am really appreciated for time. Thanks you
Best Regards
Siripong Gaewmaneechot
I would suggest looking in your config files, and the main classes which were generated when you started your laravel project (User class, etc) because they are all set to App\User App..... etc.
So for example, in the image you have in the question, it says it can not find App\AppProviders... - This indicates that somewhere you still have a use statement pointed to App\AppProviders.. but you changed the app name, so it's no longer App. something I do if I made that mistake, is I do a global search in my project files for App\ (you may need to put App\\ in the search because \ is an escape character
So if you did not change the app name immediately after starting the project, some of the paths will not be pointing to the correct directories. Let me know if that makes sense.
The command changes the PSR-4 configuration in composer.json.
Assume it was App before, your composer.json looks like this:
"autoload": {
"psr-4": {
"App\\": "app/"
}
},
After running the command with php artisan app:name Foo, it will look like:
"autoload": {
"psr-4": {
"Foo\\": "app/"
}
},
Therefore the whole namespace has changed and your classes can't be found by the Autoloader. To fix this, you have to either go back to the old name or do a global search and replace to change the namespace from App to Foo.

Laravel 4.2 , Model (eloquent) my own class not found

I have this weird error ... I apparently cannot use any of my model class in my project..
Ad_category model
class Ad_category extends Eloquent {
protected $table = 'ad_category';
protected $fillable = array('*');
use SoftDeletingTrait;
protected $dates = ['deleted_at'];
}
calling this
$ad_cat=Ad_category::find(1);
error
`Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_ERROR)
Class 'Ad_category' not found `
PHP frameworks use a system called "autoloading" to automatically include or require in the correct class definition file when you want to use a class. Autoloading in Laravel 4.2 is in a bit of a transitional spot, which means there's multiple answers to your question.
By default, Laravel 4.2 will look for a class named Ad_category in one of the following four locations.
app/commands/Ad/category.php
app/controllers/Ad/category.php
app/models/Ad/category.php
app/database/seeds/Ad/category.php
That is, Laravel's autoloader will automatically convert Ad_category into the file path Ad/category.php, and then check each configured autoload path for that file. You can configure the base autoloader paths in
#File: app/start/global.php
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
));
Laravel 4.2 also uses composer based autoloading. Specifically, is uses a very aggressive form of composer autoloading called classmap autoloading. If you look in your composer.json file, you'll see a section like this
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
When you manually run the command
$ composer dumpautoload
Composer will go through every folder in the above section and look for PHP class files. If it finds one, it adds it to the classmap in vendor/composer/autoload_classes.php. Composer also runs this command automatically during updates.
So, what this means is, if you've defined Ad_category in a different location than Laravel expects to find it, you may be able to get away with running
$ composer dumpautoload
and Laravel will use Composer's autoloader to find your class.

Laravel 5 - workbench migrations [duplicate]

I am having trouble to create package in Laravel 5 as workbench has been removed.
As in this thread (How create package in Laravel 5?), Goldorak suggest that we have to create our own package structure ourselves.
So, how can I create the workbench manually and get everything ready for package development?
Using the laravel Workbench package:
You can add the illuminate/workbench package in a Laravel 5 by adding to your composer.json:
"illuminate/workbench": "dev-master"
then add the WorkbenchServiceProvider into your config/app.php file:
'Illuminate\Workbench\WorkbenchServiceProvider'
Now you need to create the config/workbench.php file since it has been removed from Laravel 5:
<?php
return [
/*
|--------------------------------------------------------------------------
| Workbench Author Name
|--------------------------------------------------------------------------
|
| When you create new packages via the Artisan "workbench" command your
| name is needed to generate the composer.json file for your package.
| You may specify it now so it is used for all of your workbenches.
|
*/
'name' => '',
/*
|--------------------------------------------------------------------------
| Workbench Author E-Mail Address
|--------------------------------------------------------------------------
|
| Like the option above, your e-mail address is used when generating new
| workbench packages. The e-mail is placed in your composer.json file
| automatically after the package is created by the workbench tool.
|
*/
'email' => '',
];
Fill your information in this config file then you will be able to use the workbench command:
php artisan workbench vendor/name
Creating your own package structure
In this exemple we will create our package called awesome in a packages directory.
Here is the package structure:
packages/
vendor/
awesome/
src/
Awesome.php
composer.json
Vendor: your vendor name, typically this is your github username.
Awesome: the name of your package
src: Where you put the business logic
To generate a composer.json file you can use this command in the packages/vendor/awesome directory:
composer init
Now we create a Awesome.php class in the src directory with a simple method:
<?php namespace Vendor/Awesome;
class Awesome
{
public static function printAwesomeness()
{
echo 'Awesome';
}
}
After that we add the package to the laravel composer.json psr-4 autoloader:
"autoload": {
"psr-4": {
"App\\": "app/",
"Vendor\\Awesome\\": "packages/vendor/awesome/src"
}
},
and we dump the composer autoloader
composer dump-autoload
Now you can use your package everywhere in your laravel 5 project. If you need some laravel specific feature like service provider or view publishing, use them as described in the Laravel 5.0 documentation.
laravel 5 Standards with out workbench.
Set 1 : install laravel as usual.
Step 2 : Create package folder and service provider
In root directory create a folder call "packages" /"vendorName"/"packageName"/src" Eg: root/packages/jai/Contact/src
now navigate to src folder and create a service provider class: "ContactServiceprovider.php"
your service provider should extend ServiceProvider which has to implement register method.
Note:If you want you can have dd("testing"); in boot function and go to step 3 but you have copied the file you might want to create views , routes , config and controllers check link below for that
Step 3 : add package path in root composer.json in your root composer.json file "jai\Contact\": "packages/jai/Contact/src/" under psr-4
"psr-4": { "App\": "app/", "Jai\Contact\": "packages/jai/contact/src/", }
Step 4 : add service provider in app config.
in your root/conifg/app.php under providers add your package service provider to hook your package in.
'Jai\Contact\ContactServiceProvider',
Step 5 : run composer dump-autoload - make sure there are no errors.
all done - now you can access your package via url - "yourwebsite/contact"
Resource from here : https://github.com/jaiwalker/setup-laravel5-package
You could use package on this named packman. composer global require "hadefication/packman", just a simple package creator for Laravel.

Resources