Long story short is that I screwed up my authentication scaffolding and would like to know how to completely reset it to the base scaffolding. I tried deleting files however when I ran the appropriate artisan command, it did not rebuild the scaffolding.
The question is, how do I reset the scaffolding to the point where I just ran the "php artisan make:auth" command?
So, Since you're not using any version control then it has became very difficult to track the changes or going back. However you can go to /vendor/laravel/framework/src/Illuminate/Auth/Console/MakeAuthCommand.php file to see what changes php artisan make:auth does and undone things.
Here's the content of that file.
<?php
namespace Illuminate\Auth\Console;
use Illuminate\Console\Command;
use Illuminate\Console\AppNamespaceDetectorTrait;
class MakeAuthCommand extends Command
{
use AppNamespaceDetectorTrait;
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'make:auth {--views : Only scaffold the authentication views}';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Scaffold basic login and registration views and routes';
/**
* The views that need to be exported.
*
* #var array
*/
protected $views = [
'auth/login.stub' => 'auth/login.blade.php',
'auth/register.stub' => 'auth/register.blade.php',
'auth/passwords/email.stub' => 'auth/passwords/email.blade.php',
'auth/passwords/reset.stub' => 'auth/passwords/reset.blade.php',
'layouts/app.stub' => 'layouts/app.blade.php',
'home.stub' => 'home.blade.php',
];
/**
* Execute the console command.
*
* #return void
*/
public function fire()
{
$this->createDirectories();
$this->exportViews();
if (! $this->option('views')) {
file_put_contents(
app_path('Http/Controllers/HomeController.php'),
$this->compileControllerStub()
);
file_put_contents(
base_path('routes/web.php'),
file_get_contents(__DIR__.'/stubs/make/routes.stub'),
FILE_APPEND
);
}
$this->info('Authentication scaffolding generated successfully.');
}
/**
* Create the directories for the files.
*
* #return void
*/
protected function createDirectories()
{
if (! is_dir(base_path('resources/views/layouts'))) {
mkdir(base_path('resources/views/layouts'), 0755, true);
}
if (! is_dir(base_path('resources/views/auth/passwords'))) {
mkdir(base_path('resources/views/auth/passwords'), 0755, true);
}
}
/**
* Export the authentication views.
*
* #return void
*/
protected function exportViews()
{
foreach ($this->views as $key => $value) {
copy(
__DIR__.'/stubs/make/views/'.$key,
base_path('resources/views/'.$value)
);
}
}
/**
* Compiles the HomeController stub.
*
* #return string
*/
protected function compileControllerStub()
{
return str_replace(
'{{namespace}}',
$this->getAppNamespace(),
file_get_contents(__DIR__.'/stubs/make/controllers/HomeController.stub')
);
}
}
Related
could you help me with a problem that I have? I have created a command with php artisan make:command to generate repository type classes from existing models. The problem is that I need that instead of generating a single file from a stub, I need to generate 2 or more files. I can't find documentation regarding the subject. Currently what I have achieved is that I only generate a single file from a template.
<?php
namespace App\Console\Commands;
use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Input\InputArgument;
class MakeRepository extends GeneratorCommand
{
/**
* The console command name.
*
* #var string
*/
protected $name = 'make:repository';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Create a new repository';
/**
* The type of class being generated.
*
* #var string
*/
protected $type = 'Repository';
/**
* #inheritDoc
*/
protected function getStub()
{
return __DIR__ . '/stubs/MakeRepository/ModelRepositoryInterface.stub';
}
/**
* Get the console command arguments.
*
* #return array
*/
protected function getArguments()
{
return [
['name', InputArgument::REQUIRED, 'The name of the model to which the repository will be generated'],
];
}
/**
* Get the default namespace for the class.
*
* #param string $rootNamespace
* #return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.'\Repositories';
}
}
EDIT #1
I have 2 stub files inside the directory:
app/Console/Commands/stubs/MakeRepository
ModelRepository.stub
ModelRepositoryInterface.stub
I want that when you execute the command...ex: php artisan make:repository Blog, these 2 files are created in the following directory:
/app/Repositories/Blog/BlogRepository.php
/app/Repositories/Blog/BlogRepositoryInterface.php
You can write a new command to create repository interface, and then call it in MakeRepository.
I think this method is in line with SRP.
// In MakeRepository.php
// Override handle method
public function handle()
{
if (parent::handle() === false && ! $this->option('force')) {
return false;
}
if ($this->option('interface')) {
$this->call('make:repository-interface', ['name' => $this->getNameInput() . 'Interface']);
}
}
/**
* Get the console command arguments.
*
* #return array
*/
protected function getArguments()
{
return [
['name', InputArgument::REQUIRED, 'The name of the model to which the repository will be generated'],
['interface', 'i', InputOption::VALUE_NONE, 'Create a new interface for the repository'],
];
}
You can also refer to the code of make model of official.
https://github.com/laravel/framework/blob/6.x/src/Illuminate/Foundation/Console/ModelMakeCommand.php
you can use glob to get your stubs in array then loop over them to create the multiple files
foreach(glob(stubs_path('stubs/Repository/*.stub') as $stub){
copy(stubs_path('stubs/Repository/'.$stub), $repositoryLocation . 'Repository.php');
}
I'm trying to send an email using a command in Laravel. I would like to send a file from a specific folder. Previously I did it using a view with a form, but now I want to send the email using a command. The file will always be in the same folder.
This is the command code:
<?php
namespace efsystem\Console\Commands;
use Illuminate\Console\Command;
use Storage;
use Mail;
use Config;
class SendEmailEfsystem extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'emails:send';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Sending emails to the users';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$data = array(
'destino' => 'example#gmail.com',
'asunto' => 'example',
);
Mail::send('administracion.email.email_body', $data, function ($message) use ($data) {
$message->to($data['destino']);
$message->subject($data['asunto']);
$message->from(Config::get('mail.username'));
});
$this->info('The emails are send successfully!');
}
}
Since in the "form" you used $request['a_file'] the variable was an instance of Symfony\Component\HttpFoundation\File\UploadedFile wich is an extention of Symfony\Component\HttpFoundation\File\File.
what you need to do is instantiate a File class with the path you have.
$data = array(
'destino' => 'example#gmail.com',
'asunto' => 'example',
'a_file' => new \Symfony\Component\HttpFoundation\File\File($pathToFile, true)
);
You can use N69S a_file answer
This is basic tips to help you run your command.
Your Kernel.php must be like this
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
Commands\SendEmailEfsystem::class,
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('emails:send')->everyMonth();
}
/**
* Register the Closure based commands for the application.
*
* #return void
*/
protected function commands()
{
require base_path('routes/console.php');
}
}
Then if you want to run it. Simply run php artisan emails:send
or You want to run it using code you can use Artisan::call('emails:send);
i have created following command in laravel using command php artisan make:command RedisDataListener
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class RedisDataListener extends Command {
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'listen:subscribe';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Subscribe to redis channel for listening events';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct() {
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle() {
$value = cache()->has('tracking_application:mykey') ? cache()->get('tracking_application:mykey') : 'no';
print_r(array(
$value
));
}
}
but when i'm using cache()->get('key') it is not working. Same function works fine when i use in controller.
i'm using redis as the cache server.
also tried with cache::get() but nothing happen.
i set up a cronjob in cpanel, i'm using laravel 4.2. i also set to send the cronjob to my email and below yellow part is the error i received in email.
[InvalidArgumentException]
There are no commands defined in the "user" namespace.
//laravel command.php
<?php
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class CronDelFilesCommand extends Command {
/**
* The console command name.
*
* #var string
*/
protected $name = 'user:active';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Command description.';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function fire()
{
echo "aaa";
}
/**
* Get the console command arguments.
*
* #return array
*/
protected function getArguments()
{
return array(
);
}
/**
* Get the console command options.
*
* #return array
*/
protected function getOptions()
{
return array(
array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
);
}
}
//command in cpanel
* * * * * /usr/bin/php /home/project/public_html/artisan user:active > /home/project/public_html/log.txt
what's wrong? what's the error mean?
UPDATED
thanks to #KristianHareland i use wget. :)
I think you forget to add this command to artisan app/start/artisan.php.
You shoud wright:
Artisan::add(new CronDelFilesCommand());
And don't forget about composer dump-autoload
More info you can find here.
I need a custom command in my php artisan.
I have created the command file via artisan shell.
But when I execute it via php artisan it throws it as undefined.
It is also confusing for me how to add arguments to my command.
Here is my whole command file:
<?php
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class createLibrarySet extends Command
{
/**
* The console command name.
*
* #var string
*/
protected $name = 'createLibrarySet';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Creates a Library Set.';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function fire()
{
$dir = mkdir(app_path()."/".library."/".$this->library_name);
$coreName = $this->library_name;
$serivceProvider = $coreName."."."ServiceProvider.php";
$library = $coreName."."."Library.php";
$facade = $coreName."."."Facade.php";
$interface = $coreName."."."Interface.php";
fopen($dir."/".$library, "w");
fopen($dir."/".$facade, "w");
fopen($dir."/".$serivceProvider, "w");
fopen($dir."/".$interface, "w");
}
/**
* Get the console command arguments.
*
* #return array
*/
protected function getArguments()
{
return array(
array('example', InputArgument::REQUIRED, 'An example argument.'),
);
}
/**
* Get the console command options.
*
* #return array
*/
protected function getOptions()
{
return array(
array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
);
}
}
You have to add the command to your app/start/artisan.php:
Artisan::resolve('createLibrarySet');
Now you should be able to see in the
php artisan
listing.
To get your arguments, you just have to:
$email = $this->argument('email');
And options
$force = $this->option('force');