How can I schedule the pinging of routes within my application [duplicate] - php

In kohana framework I can call controller via command line using
php5 index.php --uri=controller/method/var1/var2
Is it possible to call controller I want in Laravel 5 via cli? If yes, how to do this?

There is no way so far (not sure if there will ever be). However you can create your own Artisan Command that can do that. Create a command CallRoute using this:
php artisan make:console CallRoute
For Laravel 5.3 or greater you need to use make:command instead:
php artisan make:command CallRoute
This will generate a command class in app/Console/Commands/CallRoute.php. The contents of that class should look like this:
<?php namespace App\Console\Commands;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Illuminate\Http\Request;
class CallRoute extends Command {
protected $name = 'route:call';
protected $description = 'Call route from CLI';
public function __construct()
{
parent::__construct();
}
public function fire()
{
$request = Request::create($this->option('uri'), 'GET');
$this->info(app()['Illuminate\Contracts\Http\Kernel']->handle($request));
}
protected function getOptions()
{
return [
['uri', null, InputOption::VALUE_REQUIRED, 'The path of the route to be called', null],
];
}
}
You then need to register the command by adding it to the $commands array in app/Console/Kernel.php:
protected $commands = [
...,
'App\Console\Commands\CallRoute',
];
You can now call any route by using this command:
php artisan route:call --uri=/route/path/with/param
Mind you, this command will return a response as it would be sent to the browser, that means it includes the HTTP headers at the top of the output.

I am using Laravel 5.0 and I am triggering controllers using this code:
$ php artisan tinker
$ $controller = app()->make('App\Http\Controllers\MyController');
$ app()->call([$controller, 'myMethodName'], []);
the last [] in the app()->call() can hold arguments such as [user_id] => 10 etc'

For Laravel 5.4:
php artisan make:command CallRoute
Then in app/Console/Commands/CallRoute.php:
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Http\Request;
class CallRoute extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'route:call {uri}';
/**
* The console command description.
*
* #var string
*/
protected $description = 'php artsian route:call /route';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$request = Request::create($this->argument('uri'), 'GET');
$this->info(app()->make(\Illuminate\Contracts\Http\Kernel::class)->handle($request));
}
}
Then in app/Console/Kernel.php:
protected $commands = [
'App\Console\Commands\CallRoute'
];
Call like: php artisan route:call /path

Laravel 5.7
Using tinker
// URL: http://xxx.test/calendar?filter[id]=1&anotherparam=2
$cc = app()->make('App\Http\Controllers\CalendarController');
app()->call([$cc, 'getCalendarV2'], ['filter[id]'=>1, 'anotherparam' => '2']);

You can do it in this way too. First, create the command using
php artisan command:commandName
Now in the handle of the command, call the controller and trigger the method.
Eg,
public function handle(){
$controller = new ControllerName(); // make sure to import the controller
$controller->controllerMethod();
}
This will actually do the work. Hope, this helps.
DEPENDENCY INJECTION WON'T WORK

To version 8 of laravel.
First step: type command in terminal
php artisan tinker
Secound step:
$instante = new MyController(null);
Or if argument by an instance of model, then, pass name model class.
Example:
$instante = new MyController(new MyModelHere());
Press enter.
Finally, call method with $instante->myMethod() here.
See:

Related

why my cron job is not working in laravel?

in db i have column visit_clear i want it 0 after one day so i used this code
in kernal.php
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
protected $commands = [
];
protected function schedule(Schedule $schedule)
{
$schedule->command('cron:update-user-not-new')->daily();
}
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
and in command/UpdateUserNotNew.php
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class UpdateUserNotNew extends Command
{
protected $signature = 'cron:update-user-not-new';
protected $description = 'Command description';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$dayAgo = 1; // Days ago
$dayToCheck = \Carbon\Carbon::now()->subDays($dayAgo)->format('Y-m-d');
Customer::whereDate('visit_date', '<=', $dayToCheck)
->update([
'visit_clear' => 0
]);
}
}
i am sheduling commnd like this as u can see cron:update-user-not-new should i use crone:UpdateUserNotNew?
You need to register your command in Kernel.php like this:
protected $commands = [
'App\Console\Commands\UpdateUserNotNew',
];
You should then be able to run the command manually with php artisan cron:update-user-not-new
In order for the automatic running of the command to work, you need to add an entry to your system's task scheduler, as this is what Laravel uses to run commands on a schedule.
Assuming you are using Linux, you need to add an entry to your crontab. To do this, in a command prompt enter crontab -e, hit enter, and add this line:
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
Don't forget to replace /path-to-your-project with the root folder of your project
Once done editing the crontab, save and close the editor and the new entries should be installed, and your command should now run on the schedule.
All this info came from https://laravel.com/docs/7.x/scheduling so if you need more info take a look there

Access Arguments Input via Artisan Console Command

I want to run my command like this
php artisan update:code --code=123
I want to get the code from first argument - I can't seems to find a way to do it on Laravel site.
<?php
namespace App\Console\Commands;
use App\User;
use Illuminate\Console\Command;
class updateCode extends Command
{
protected $signature = 'update:code {code}';
protected $description = 'Update Code ... ';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$code = $this->option('code');
$this->info($code);
$user = User::where('type','Admin')->first();
$user->code = bcrypt($code);
$user->active = 1;
$user->save();
$this->info($user);
}
}
I kept getting
The "--code" option does not exist.
Do I need to defind my option too ?
How can I just quickly access the first argument ?
{code} is for arguments. For options it is {--code} or {--code=}:
// option as switch:
protected $signature = 'update:code {--code}';
// option with value:
protected $signature = 'update:code {--code=}';
Just use
php artisan update:code 123

Laravel Command argument returns keyvalue pair insted of value

I've generate Laravel artisan command by php artisan make:command SomeCommand. Here is the entire command class SomeCommand.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class SomeCommand extends Command{
protected $signature = 'Call:SomeCommand {phone="8980131488"} {name="Kiran Maniya"}';
protected $description = 'This is a buggy Command';
public function __construct(){
parent::__construct();
}
public function handle(){
$args = $this->arguments();
$this->info($args['phone'].' '.$args['name']);
}
}
The issue is, when i call the command by php artisan Call:SomeCommand phone="8980151878" name="Anubhav Rane". It outputs the arguments with keypair value as name=Anubhav Rane & phone=8980151878. It should only output the values.
I also tried catching single values by $this->argument('phone') and $this->argument('name') but still it outputs the same.
The way you're passing the arguments is incorrect. Try this:
php artisan Call:SomeCommand 8980151878 'Anubhav Rane'

Laravel 5.5 Model not found in Seed class

I have been around this problem for so long and cannot solve it... I found several people with (apparently) the same problem as me, but any of the answers helped me.
I have the following "Sector.php" inside "app" folder:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Sector extends Model
{
protected $table = 'sectors';
protected $fillable = ['name'];
protected $guarded = ['id'];
public function services()
{
return $this->belongsToMany('App\Service', 'services_sectors', 'sector_id', 'service_id');
}
public function observations()
{
return $this->belongsToMany('App\Observation', 'observations_sectors', 'sector_id', 'observation_id');
}
}
And the following "DatabaseSeeder.php" inside "database/seeds":
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
DB::table('sectors')->delete();
Sector::create(['name' => 'Health']);
$this->command->info('Sectors table seeded');
}
}
So, when I access my server I run the command php artisan db:seed but I have the following error:
[Symfony\Component\Debug\Exception\FatalThrowableError]
Class 'Sector' not found
I have been trying ./composer update, ./composer dump-autoload -o, changing Sector to App\Sector in the seeder file but the error just changes to Class 'App\Sector' not found.
If I add use App\Sector; to the top of the Seeder file the error is the same.
It seems I tried all the solutions that are online, so maybe I have some configuration done incorrectly? Any suggestions on this?
Try adding use App\Sector; to your seeding file.
Once you have it working, think about separating your seeding files into their separate classes. It is much easier to maintain that way.
Generate Seeder File
First, in terminal, generate a new seed file:
php artisan make:seeder SectorsTableSeeder
Transfer your seeding code into the run method of this new file.
Call seeder files
Then, modify the DatabaseSeeder.php file to run the SectorsTableSeeder class. For example:
public function run()
{
$this->call(SectorsTableSeeder::class);
}
Update
Sorry, I missed that part.
This is what I would try:
$now = date('Y-m-d H:i:s');
public function run()
{
DB::table('sectors')->delete();
DB::table('sectors')->insert([
'name' => 'Health',
'created_at' => $now,
'updated_at' => $now,
]);
$this->command->info('Sectors table seeded');
}

How to get laravel command arguments outside command, e.g. in service provider?

I can get arguments in command with this code:
$this->argument();
But how to get arguments outside ?
If I look at source of argument() function I see :
public function argument($key = null)
{
if (is_null($key)) {
return $this->input->getArguments();
}
return $this->input->getArgument($key);
}
I want to detect when command "php artisan migrate:refresh --seed" is running because I want some part of code in models run at localhost enviroment but not in localhost enviroment during seeding...
Mechanism how laravel gets command arguments is pretty complicated. I can detect if app is running in console with \App::runningInConsole() but there is no function which will get arguments, something like :
if(\App::runningInConsole()){
$args = \App::getConsoleArguments(); // doesn't exist :(
}
but $_SERVER['argv'] can be usefull here, when "php artisan migrate:refresh --seed" is running in $_SERVER['argv'] is this array:
Array
(
[0] => artisan
[1] => migrate:refresh
[2] => --seed
)
so I can use this code:
if( ! empty($_SERVER['argv'][2] ) && $_SERVER['argv'][2] == '--seed'){
//
}
I has the same problem building a Laravel SAAS app on AWS, in base to this project I modify my ServiceProvider for this:
<?php
namespace App\Providers;
use Illuminate\Console\Events\ArtisanStarting;
use Illuminate\Support\ServiceProvider;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\EventDispatcher\EventDispatcher;
class MultitenantServiceProvider extends ServiceProvider{
protected $consoleDispatcher = false;
protected $commands_with_tenant = [
'migrate', 'migrate:refresh', 'migrate:install', 'migrate:reset', 'migrate:rollback',
'migrate:status', 'passport:client', 'passport:install', 'passport:keys'
];
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot(){
if( $this->app->runningInConsole() ){
$this->registerTenantOption();
$this->verifyTenantOption();
}
// Multitenant re-configure in case of HTTP request
}
/**
* Register any application services.
*
* #return void
*/
public function register(){
$this->app->singleton('multitenant', function ($app){
// Register your Multitenant
});
}
protected function registerTenantOption(){
$this->app['events']->listen(ArtisanStarting::class, function($event){
$definition = $event->artisan->getDefinition();
$definition->addOption(
new InputOption('--tenant', null, InputOption::VALUE_OPTIONAL, 'The tenant subdomain the command should be run for. Use * or all for every tenant.')
);
$event->artisan->setDefinition($definition);
$event->artisan->setDispatcher($this->getConsoleDispatcher());
});
}
protected function verifyTenantOption(){
$this->getConsoleDispatcher()->addListener(ConsoleEvents::COMMAND, function(ConsoleCommandEvent $event){
if( in_array($event->getCommand()->getName() , $this->commands_with_tenant) ){
$tenant = $event->getInput()->getParameterOption('--tenant', null);
if (!is_null($tenant)){
if ($tenant == 'all'){
// Do something with 'all'
}
else{
// Do something with $tenant
}
}
else{
$event->getOutput('<error>This command need that specified a tenant client</error>');
$event->disableCommand();
}
}
});
}
protected function getConsoleDispatcher(){
if (!$this->consoleDispatcher){
$this->consoleDispatcher = app(EventDispatcher::class);
}
return $this->consoleDispatcher;
}
In this class, there is an array with the commands that are needed to verify and use a multitenant config.

Categories