Generate files using custom artisan make command Laravel - laravel

I am trying to generate a class file using custom artisan make command.My command is showing under artisan make but I am not able to generate file
What i did
1.Use php artisan make:command CreateActionClass and implement GeneratorCommand
<?php
namespace App\Console\Commands;
use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Input\InputArgument;
class CreateActionClass extends GeneratorCommand
{
protected $signature = 'make:action {name}';
protected $description = 'Create New Action Single Responsibility';
protected $type = 'Action';
public function handle()
{
//
}
protected function getStub()
{
return app_path().'/Console/Stubs/MakeActionStub.stub';
}
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.'\Actions';
}
protected function getArguments()
{
return [
['name', InputArgument::REQUIRED, 'The name of the contract.'],
];
}}
2. Generate .stub file /Console/Stubs/MakeActionStub.stub
<?php
namespace DummyNamespace;
class DummyAction
{
}
Please Help

Remove the handle method. It is overriding the GeneratorCommand class's handle that's why its doing nothing. If you wish to extend the handle method, call parent::handle() before or after your statements

Related

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 and Local Tunnel integration

My idea was to use Local Tunnel's subdomain feature to expose callback URI in a more convenient way. However, I believe that I could've achieved the same results in a simper way.
This is the solution with Laravel Valet:
In package.json I've added a script called shared
"scripts": {
...
"share": "lt --port 80 --subdomain blog --local-host blog.test"
}
In AppServiceProvider.php I've extended the UrlGenerator to avoid redirecting back to http://blog.test
<?php
namespace App\Providers;
use App\Services\LocalTunnelUrlGenerator;
use Blade;
use Illuminate\Http\Resources\Json\Resource;
use Illuminate\Routing\Router;
use Illuminate\Routing\UrlGenerator;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
(...)
}
public function register()
{
$this->enableLocalTunnel();
}
private function enableLocalTunnel()
{
if (!app()->environment('local') || !config('app.use_local_tunnel')) {
return;
}
$this->app->extend('url', function (UrlGenerator $defaultGenerator) {
/** #var Router $router */
$router = $this->app['router'];
$routes = $router->getRoutes();
return new LocalTunnelUrlGenerator($routes, $defaultGenerator->getRequest());
});
}
}
This is the the LocalTunnelUrlGenerator.php:
<?php
namespace App\Services;
use Illuminate\Http\Request;
use Illuminate\Routing\RouteCollection;
use Illuminate\Routing\UrlGenerator;
class LocalTunnelUrlGenerator extends UrlGenerator
{
public function __construct(RouteCollection $routes, Request $request)
{
parent::__construct($routes, $request);
}
public function formatRoot($scheme, $root = null)
{
return "https://blog.localtunnel.me";
}
}
Why all that? Because whenever the application call the redirect() method, we are sent back to http://blog.test.
Do I really need to extend the UrlGenerator to make it work?

How can I call command schedule via url on the laravel?

I using laravel 5.6
I set my schedule in the kernel.php like this :
<?php
namespace App\Console;
use App\Console\Commands\ImportLocation;
use App\Console\Commands\ImportItem;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
protected $commands = [
ImportLocation::class,
ImportItem::class,
];
protected function schedule(Schedule $schedule)
{
$schedule->command('inspire')->dailyAt('23:00');
}
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
So there are two command
I will show one of my commands like this :
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\Location;
class ImportLocation extends Command
{
protected $signature = 'import:location';
protected $description = 'import data';
public function __construct()
{
parent::__construct();
}
public function handle()
{
...
}
}
I want to run the command via url. So it not run in the command promp
I try like this :
I add this script : Route::get('artisan/{command}/{param}', 'CommandController#show');
in the routes and I make a controller like this :
namespace App\Http\Controllers;
class CommandController extends Controller
{
public function show($command, $param)
{
$artisan = \Artisan::call($command.":".$param);
$output = \Artisan::output();
return $output;
}
}
And I call from url like this : http://myapp-local.test/artisan/import/location
It works. But it just run one command
I want to run all command in the kernel. So run import location and import item
How can I do it?
What you can do is register a custom method in your Kernel.php to retrieve all custom registered commands in the protected $commands array:
public function getCustomCommands()
{
return $this->commands;
}
Then in your controller you can loop them all and execute them via Artisan's call() or queue() methods:
$customCommands = resolve(Illuminate\Contracts\Console\Kernel::class)->getCustomCommands();
foreach($customCommands as $commandClass)
{
$exitCode = \Artisan::call($commandClass);
//do your stuff further
}
More on Commands you can understand on the documentation's page

call a model from a command class in Laravel 5, and pass the command class itself

I have a laravel 5.5 artisan command working, so of course I can use methods like $this->info() and $this->arguments() etc.. it looks like this:
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use Compasspointmedia\Julietmenu\Model\Menu;
class MenuManagementCommand extends Command
{
/**
* The console command name.
*
* #var string
*/
protected $signature = 'julietmenu:menu
{action=list}';
protected $description = 'Manages menu elements per the Juliet CMS specification';
public function __construct() {
parent::__construct();
//trying to make this command methods available in Menu
$this->menu = new Menu($this);
}
/**
* Execute the console command.
*/
public function handle()
{
// this works just fine
$this->info('Calling ' . $this->argument('action'));
$action = $this->argument('action');
$this->menu->action();
}
}
Of course, I would like do the actual work in the Model, not the command, using the command like a controller. Here's the model class so far:
namespace Compasspointmedia\Julietmenu\Model;
use Illuminate\Database\Eloquent\Model;
class Menu extends Model {
//
public function __construct($command){
$this->command = $command;
// this says the `arguments` method is present:
print_r(get_class_methods($this->command));
// but, it does not work!
// error: "Call to a member function getArguments() on null"
$this->arguments = $this->command->arguments();
}
public function node(){
echo '--- it works! -----';
}
}
To the point, how do I pass the Command object to the Model so that I can use $this->command->arguments() or the other Command features inside the model?
P.S. I'd be very grateful to know if there's a native "Laravel way" to do this better than passing the entire $this to a constructor.

laravel 5.1 - trait boot not being called for model::update() function

I have created trait as follows on this page app/Traits/ModelEventThrower.php
namespace App\Traits;
use Input;
use Event;
use App\Events\ActivityLog;
use Illuminate\Database\Eloquent\Model;
//use Illuminate\Support\Facades\Event;
/**
* Class ModelEventThrower
* #package App\Traits
*
* Automatically throw Add, Update, Delete events of Model.
*/
trait ModelEventThrower {
/**
* Automatically boot with Model, and register Events handler.
*/
protected static function bootModelEventThrower()
{
foreach (static::getModelEvents() as $eventName) {
static::$eventName(function (Model $model) use ($eventName) {
try {
$reflect = new \ReflectionClass($model);
echo "here";exit;
} catch (\Exception $e) {
return true;
}
});
}
}
/**
* Set the default events to be recorded if the $recordEvents
* property does not exist on the model.
*
* #return array
*/
protected static function getModelEvents()
{
if (isset(static::$recordEvents)) {
return static::$recordEvents;
}
return [
'created',
'updated',
'deleted',
];
}
}
My City Model is something like this
namespace App;
use App\Traits\ModelEventThrower;
use App\Events\ActivityLog;
use Illuminate\Database\Eloquent\Model;
use Event;
class City extends Model
{
use ModelEventThrower;
//protected static $recordEvents = ['updated'];
...
}
My CitiesController is
namespace App\Http\Controllers\Admin;
use App\City;
use App\Country;
use Input;
use Validator;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class CitiesController extends Controller
{
......
public function update(City $city,Request $request)
{
......
$city->where('id','=',$input['id'])->update($input);
Somehow, I dont see its calling the function written in trait file. When I tried to create $city->create($input); it echos "here" and stops execusion, but not doing same for update function , however I could successfully update the records.
Any suggestion/help will be highly appreciated.
I had a similar issue with Laravel. By adding a constructor in the model to call the boot() function of the parent Model, like so:
public function __construct()
{
parent::boot();
}
you can make sure that all the traits are booted. This solved it for me.

Resources