Custom Laravel Artisan Make Controller command - laravel

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.

Related

Laravel: How to add signature option to migrate:rollback command?

I have multiple databases and the migrations for each database are stored in a different folder. Hence, I want to override the migrate:rollback command and add an option for a folder instead of specifying the path every time.
So instead of running the following command:
php artisan migrate:rollback --path=/database/migrations/{{folder}}
I want to run:
php artisan migrate:rollback {{folder}}
How can I achieve this?
protected $signature = 'test {folder}';
protected $description = 'Testing Purpose';
public function handle()
{
$this->ask('Enter Folder Name?');
$folder = $this->argument('folder');
$command = "migrate:rollback --path=/database/migrations/{$folder}";
Artisan::call($command);
}

Laravel: how to simplify artisan commands?

I want to simplify the following artisan commands because I have several databases and the migrations for each database are stored in a separate folder.
php artisan make:migration {{name}} --path=/database/migrations/{{folder}}
php artisan migrate:rollback --path=/database/migrations/{{folder}}
to
php artisan make:migration {{name}} {{folder}}
php artisan migrate:rollback {{folder}}
Is this possible and if so how can I implement it?
Since this is not an option in the Laravel commands, a way to implement this yourself is by writing you own commands that call other artisan commands.
To do so, in your terminal write, for example, php artisan make:command MigrateWithPath to create a new command at app/Console/Commands/MigrateWithPath.php. Then, you can call the vanilla implementation Laravel provides at (vendor\laravel\framework\src) \Illuminate\Database\Console\Migrations\MigrateMakeCommand but then in a way that you specify.
Be sure though that the name of your new command needs to be different from the Laravel one, to prevent recursions. Therefore, I have prefixed the name with app: to be like app:make:migration, but feel free to use something else.
Take a look at the following suggestion:
class MigrateWithPath extends BaseCommand
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'app:make:migration {name : The name of the migration}
{folder? : The location where the migration file should be created}';
/**
* Execute the console command.
*
* #return int
*/
public function handle()
{
$this->call('make:migration', [
'name' => $this->argument('name'),
'--path' => '/database/migrations/' . $this->argument('folder'),
]);
return 0;
}
Then do the same for the rollback command.

Laravel custom artisan command not listed

I have several artisan commands which I wrote.
All of them share common functionality, so instead of extending Command class, I wrote a MyBaseCommand class so all commands extend this one:
namespace App\Console\Commands;
use Illuminate\Console\Command;
class SomeCommand extends MyBaseCommand
{
protected $signature = 'mycommands:command1';
protected $description = 'Some description';
:
:
And the base class:
namespace App\Console\Commands;
class MyBaseCommand extends Command
{
:
:
The problem is that from some reason these commands are no longer listed with php artisan.
Any idea how can I force laravel to list these commands as well?
protected $signature = 'mycommands:command1'; //this is your command name
Open app\Console\kernel.php file.
protected $commands = [
\App\Console\Commands\SomeCommand::class,
]
then run
php artisan list
Laravel tries to register the commands for you automatically with:
/**
* Register the commands for the application.
*
* #return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
You can find this in the App\Console\Kernel.php
Make sure that your classes have a signature and description property.
It is quite stupid, anyway since it might happen to someone else I leave here the answer:
I wanted to hide the base class, so I had inside it this line:
protected $hidden = true;
Of-course, the value of this variable was propagated to the high-level class, what made the custom commands hidden.
The solution is simply to add to these files this line:
protected $hidden = false;
====================== UPDATE ======================
As #aken-roberts mentions, a better solution is simply making the base class abstract:
namespace App\Console\Commands;
abstract class MyBaseCommand extends Command
{
abstract public function handle();
:
:
In this case artisan doesn't list it, and it cannot be executed.

Laravel 5.5 Package Commands Won't Register

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.

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