Laravel 5.5 Package Commands Won't Register - laravel

I have a package for Laravel 5.5 and in the CommandsServiceProvider boot() method, I have:
if($this->app->runningInConsole()){
$this->commands([
MyCommandClass:class,
]);
}
Then, my MyCommandClass looks like:
<?php
namespace Testing\Commands;
use Illuminate\Console\Command;
class MyCommandClass extends Command
{
protected $signature = "execute:test";
protected $description = "Description of the command";
public function __construct()
{
parent::__construct();
}
public function handle()
{
$this->info('Command Executed');
}
}
The issue is that Artisan does not list the command when I run php artisan and when I try to run the command with php artisan execute:test it tells me the command is not defined.
What am I missing here? I followed the documentation for registering package commands in Laravel 5.5

It would appear that the Auto discovery only works when pulling a package from a Git Repo via Composer. When developing a package, the composer files within the package do not seem to auto load.

Related

Custom Laravel Artisan Make Controller command

I want to put my controller generated by the artisan command to a custom directory. I made own command
php artisan make:command ApiControllerMake
and extended it
class ApiControllerMake extends ControllerMakeCommand
then I removed everything from there and overridden method
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.'\Http\AppAPI\Controllers';
}
It's working OK.
Then I overridden
protected $signature = 'make:api-controller';
and after run
php artisan make:api-controller MyNewController
I got error
No arguments expected for "make:api-controller" command, got "MyNewController".
What is the problem?
Take a look at the ControllerMakeCommand, they use
protected $name = 'make:controller';
You probably have this:
protected $signature = 'make:api-controller';
So in your new class, replace $signature with $name.

Is there a way to disable artisan commands?

Is there a way to disable artisan commands from running at all?
For example, if I wanted to disable php artisan migrate:fresh from running, where would I go to remove/disable the command?
As far as I know, laravel does not have this feature by default. And this is still under laravel ideas.
I also had this problem before and could not find a solution, I am not sure why you want to disable a command. But my case was that in the production environment I never want to run php artisan migrate:fresh. So what I end up with is to override the default command.
For example, in the routes/console.php file:
if ('production' === App::environment()) {
Artisan::command('migrate:fresh', function () {
$this->comment('You are not allowed to do this in production!');
})->describe('Override default command in production.');
}
So, when you are in production, php artisan migrate:fresh will do nothing. You can change the condition based on your requirement, my example is just an idea of how you can override a laravel default command based on some variables in the .env file.
You can do a lot of things here as well, I am not sure why you want to disable the command, so this is the best I can help.
Create a command like the following
<?php
namespace App\Console\Commands\Utils;
use Illuminate\Console\Command;
use Illuminate\Console\Events\CommandStarting;
class PreCommand extends Command
{
protected $signature = 'precommand';
public function handle(CommandStarting $event) {
if (app()->environment(['production'])) {
logger('Trying to fresh database in production');
if ($event->command == 'migrate:fresh') {
$this->output = $event->output;
$this->info('You can not fresh database in the production');
die();
}
}
}
}
And register it in your EventServiceProvider's boot method
<?php
namespace App\Providers;
use App\Console\Commands\Utils\PreCommand;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Console\Events\CommandStarting;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;
class EventServiceProvider extends ServiceProvider
{
/**
* Register any events for your application.
*
* #return void
*/
public function boot() {
Event::listen(CommandStarting::class, PreCommand::class);
}
}

execute Queue job without paasing by my ViewComposer i share with (*) using pm2

trying to prevent queued job from passing by ViewComposer in laravel using pm2.
Problem : setCookie has not been applied because (i think ) there is no session from extern calls access (pm2).
let me explain:
also sharing with you this part of my app.
I have a queued job that sends dynamic emails (templates and mail params)
in my ServiceController,
also, am sharing needed data with all view when returning to any of them,
so I created a ViewComposer called by any view(*) in AppServiceProvider, inside this ViewComposer, am using a trait that uses many traits too, to collect data I need for views.
when I navigate in my application everything looks fine.
when implementing any task that uses queued jobs, in my case sending an email:
when I run php artisan queue:work everything works fine too and I receive emails.
BUT
when I use pm2 deamon which calls that queue it fires an exception :
(setCookie has not been applied) related to that trait I use in view composer.
I know it's hard for me to explain it, but I really need some help here.
here is my code :
pm2 path : / (server root)
app path :/prod
shared hosting with ssh.
1. AppServiceProvider
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
Schema::defaultStringLength(191);
}
public function boot()
{
view()->composer('*',
'App\Http\ViewComposers\MasterComposer');
}
}
2. WebsiteComposer:
namespace App\Http\ViewComposers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use App\Website;
use other_traits;
trait WebsiteComposer
{
use other_traits;
protected function Get_Website(Request $request)
{
//some code by calling other traits;
// $results= get client websitestuff;
$this->website= $results;
}
}
3. MasterComposer:
namespace App\Http\ViewComposers;
use Illuminate\View\View;
use Illuminate\Http\Request;
class MasterComposer
{
use WebsiteComposer;
public $web=[];
public function __construct()
{
}
public function compose(View $view)
{
//to prevent external views( email templates located in
//views/emails from passing by getting website process...
if(!starts_with($view->getName(), 'emails'))
{
//here where exception fired using pm2
$this->Get_Website(request());
$this->web=$this->website;
}
View::share('website', $this->web);
}
}
4. ServiceContorller:
(the Start of the process)
// POST PROCESS then Calling this function :
public function EmailDispatcher($data,Request $request)
{
//some code...
dispatch(function () use ($data) {
dispatch(new GuestMailerJob($data));
});
return redirect('somview');
}
5. GuestMailerJob:
namespace App\Jobs;
use App\Mail\GuestMailer;
// needed uses...
class GuestMailerJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable,
SerializesModels,GuestMailer;
protected $data;
public $tries = 3;
public $timeout = 10;
public function retryUntil()
{
return now()->addSeconds(12);
}
public function __construct(Array $data)
{
$this->data=$data;
}
public function handle()
{
$this->sendEmail($this->data);
}
}
6. GuestMailer:
namespace App\Mail;
use Illuminate\Http\Request;
use Mail;
trait GuestMailer
{
protected function sendEmail(array $all)
{
$toview = array(
'title' => $all["title"],
'other_attributes' => $all["other_attributes"]
);
Mail::send( [$all["template"]=> $all['view']],$toview,
function ($message) use($all)
{
$message->from( $all['from'],$all['nameFrom'] );
$message->subject( $all['subject'] );
//other params...
}
});
}
}
7. pm2 executed file : mail-worker.yml
//located in: (/prod)
//mail-worker.yml content:
apps:
- name: mail-worker
script: artisan
exec_mode: fork
interpreter: php
instances: 1
args:
- queue:work
- --tries=1
- --sleep=1
8 : artisan Cleaning
all php artisan cleaners are implemented
ex : php artisan clear-compiled
php artisan queue:restart
9 : pm2 Configuration :
pm2 start mail-worker.yml
pm2 monit
10 - pm2 state :
id name mode reload status cpu memory
0 mail-worker fork 1 online 0% 6.5mp
it's working with php artisan queue:work / listen
but with pm2 here is the exception.
pm2 Result :
Whoops\Run::handleError("Trait method setCookie has not been applied,
/app_base_path/PRODMAAN/vendor/filp/whoops/src/Whoops/Run.ph x
Whoops\Run::handleShutdown() [internal]:0
failed_Jobs result :
Illuminate\Queue\MaxAttemptsExceededException: App\Jobs\GuestMailerJob has been attempted too many times or run too long. The job may have previously timed out.
what is the problem ?
thank you for any advice about the code structure too.

php artisan vendor:publish mervick/emojionearea

In installation no describe process publishing this package.
But I would like to setup it.
Laravel 5.6
composer require mervick/emojionearea ^3.0.0
and I need to copy from folder /vendor to /public/vendor using
php artisan vendor:publish
I created file /vendor/mervick/emojioneareaServiceProvider.php
and added lines:
public function boot()
{
$this->publishes(
[
__DIR__ . '/dist' =>
public_path('vendor/mervick/emojionearea/dist'),
],
'emojionearea'
);
}
also, I added lines to /config/app.php
//ServiceProviders
Mervick\EmojioneArea\EmojioneAreaServiceProvider::class,
//Aliases
'EmojioneArea'=> Mervick\EmojioneArea\EmojioneAreaServiceProvider::class,
and run command :
php artisan vendor:publish
also, I used the command:
php artisan vendor:publish --provider="Mervick\EmojioneArea\EmojioneAreaServiceProvider"
Any help.Thanks.
Please follow below steps i have tried moved files to public/vendor folder by updating below steps. Its works fine.
Service provider file.
<?php
namespace mervick\emojionearea;
use Illuminate\Support\ServiceProvider;
class EmojioneAreaServiceProvider extends ServiceProvider
{
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
$this->publishes([
__DIR__.'/../dist' => base_path('public/vendor/dist'),
]);
}
}
In your root composer.json file add your vendor for identify the service provider.
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/",
"Mervick\\EmojioneArea\\": "vendor/mervick/emojionearea/src"
}
}
Like you said don't forgot to add the service provider in config/app.php
Mervick\EmojioneArea\EmojioneAreaServiceProvider::class,
If everything works fine for you. Please make it as correct answer.:-)

Laravel 5 getstream.io getUserFeed()

I'm trying to use getstream.io in my Laravel 5 application. I'm following the tutorial here, but got stuck on this one:
$feed = FeedManager::getUserFeed($user->id);
When I go to the FeedManager class, I couldn't find the getUserFeed() method. Here's how my FeedManager class look like:
<?php namespace GetStream\StreamLaravel\Facades;
use Illuminate\Support\Facades\Facade;
class FeedManager extends Facade {
/**
* Get the registered name of the component.
*
* #return string
*/
protected static function getFacadeAccessor() { return 'feed_manager'; }
}
I wonder if I did something wrong during installation. The tutorial said to run php artisan config:publish get-stream/stream-laravel, but I did php artisan vendor:publish get-stream/stream-laravel. The reason is because I got an error when running config:publish, so I used vendor:publish instead
The Stream-PHP-Example is now working in Laravel 5, take a look: https://github.com/GetStream/Stream-Example-PHP

Resources