When I install Pusher package, I got an error "Class 'Pusher' not found".
Claudio's diagnosis is correct, the namespace Pusher was added in version 3; but changing the Laravel files is not a recommended solution.
A better way is to create an alias in config/app.php. Under the 'aliases' key, add this to the array in the "Third Party Aliases" section:
'Pusher' => Pusher\Pusher::class,
(OP posted the following answer in the question. The underlying issue is that version 3 of pusher-php-server introduces a namespace and so now requires use Pusher\Pusher.)
Create this command:
namespace App\Console\Commands;
use Illuminate\Support\Facades\File;
use Illuminate\Console\Command;
class FixPusher extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'fix:pusher';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Fix Pusher namespace issue';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$broadcastManagerPath = base_path('vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastManager.php');
$pusherBroadcasterPath = base_path('vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php');
$contents = str_replace('use Pusher;', 'use Pusher\Pusher;', File::get($broadcastManagerPath));
File::put($broadcastManagerPath, $contents);
$contents = str_replace('use Pusher;', 'use Pusher\Pusher;', File::get($pusherBroadcasterPath));
File::put($pusherBroadcasterPath, $contents);
}
}
Then add "php artisan fix:pusher" to composer.json file:
"post-update-cmd": [
"php artisan fix:pusher",
"Illuminate\\Foundation\\ComposerScripts::postUpdate",
"php artisan optimize"
]
With the version 3 of Pusher I realized that there is changed the namespace for Pusher\Pusher. If configure by composer when set it out the .env, BROADCAST_DRIVER=pusher, it's showing that error. Checking out the log, you can find out where is the proble, located at this file:
'vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastManager.php"
. It's necessary change the references for Pusher\Pusher instead of Pusher like the image:
then find out the function PusherBroadCaster and change the reference Pusher for Pusher\Pusher.
vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php
Use this command and install pusher. It worked for me in Laravel and PHP 8.0.8 perfectly without any additions of code changes.
composer require pusher/pusher-php-server
It's worth checking the version of Pusher used in composer.json. At this moment if you got stuck with implementing Pusher in Laravel 8 or 9 then your pusher-php-server version should be close to 7. At this moment the version of pusher is 7.1.0 (beta)* and therefore you should change composer.json entry to
"pusher/pusher-php-server": "^7.0",
Check this out: https://packagist.org/packages/pusher/pusher-php-server
Just go to vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php
and change "Use Pusher" to "Use Pusher/Pusher";
Related
I have this Command in the Kernel class :
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('products:import')
->appendOutputTo(storage_path('logs/import.log'));
}
The method creates a file named import.log that contains the logging that I added while importing the products, and it works fine. But I need to be able to just run php artisan products:import directly and get the file created too without calling the schedule method in the Kernel class, or using php artisan schedule:run.
I didn't find in the docs either. What should I add to php artisan products:import to get the file created?.
EDIT : if I run php artisan schedule:run, the log file gets created because of the appendOutputTo(storage_path('logs/import.log'));, but if I run php artisan products:import directly, the log file isn't created. I need to know what should I add to the php artisan products:import command line to make it work in this case too.
Override run method for your command and configure the $output stream to use a StreamOutput instead of a ConsoleOutput.
use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Illuminate\Console\Command;
class ImportCommand extends Command
{
function run(InputInterface $input=null, OutputInterface $output=null) {
$output = new StreamOutput(fopen(storage_path('logs/import.log'), 'a', false));
parent::run($input, $output);
}
function handle() {
echo "Do stuff";
}
}
I want to make an alias like
php artisan go
instead of
php artisan serve
I will appreciate any other idea :-) .I also read this link and search a lot but it wasn't so clear and other questions were about making class or .env files and etc.
Thanks in advance
Update
This question is not duplicate of this because it's not contain calling php artisan itself.
Create the command using:
php artisan make:command GoCommand
Add this in the class:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Symfony\Component\Console\Output\ConsoleOutput;
class GoCommand extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'go';
/**
* 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 handle()
{
$output = new ConsoleOutput;
$output->writeln("Laravel development server started: <http://127.0.0.1:8000>");
Artisan::call('serve');
Artisan::output();
}
}
Use the command:
php artisan go
Visit: http://127.0.0.1:8000/
and see the output in your console.
I'm running a 5.7 laravel project and I can send emails from controllers with no issue.
However, when I'm trying to use the same logic to send emails from a command, launched from the command line, the emails are not sent and I get the following error :
In AbstractSmtpTransport.php line 445:
Expected response code 220 but got an empty response
Here is my command code :
<?php
namespace App\Console\Commands;
use App\Email_confirmation;
use Illuminate\Console\Command;
use App;
use Carbon\Carbon;
use Illuminate\Support\Facades\Mail;
use App\Mail\ShopOrderConfirmation;
class sendEmailConfirmations extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'command:sendEmailConfirmations';
/**
* 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 handle()
{
$ec_global = Email_confirmation::where("due_date" ,">", Carbon::now('Europe/Paris'))->get();
if (!$ec_global->isEmpty()) {
foreach ($ec_global as $ec) {
if (App::environment('production')) {
$subject = $ec->shop_name . " - Order confirmation";
Mail::to($ec->contact_email)
->send(new ShopOrderconfirmation($ec->contact_name, $subject, $ec->shop_name, $ec->order_date));
}
elseif (App::environment('development','test')) {
$subject = $ec->shop_name . " - Order confirmation - TEST";
Mail::to("me#whatever.net")
->send(new ShopOrderconfirmation($ec->contact_name, $subject, $ec->shop_name, $ec->order_date));
}
}
}
else {
$this->info('Empty.');
}
}
}
The project is running the 6.0.2 version of the swiftmailer package. I can't find the reason why the behaviour is different here.
Please use the SMTP for sending emails with Swiftmailer
MAIL_DRIVER=smtp
Run env in your app directory to check if the Environment variables are getting through.
Make sure your MAIL_DRIVER property in .env is set to smtp.
You could also try tls for MAIL_ENCRYPTION too.
EDIT:
I just looked into SwiftMailer's AbstractSmtpTransport.php file and around line 445 there's this:
if (!$response) {
$this->throwException(new Swift_TransportException('Expected response code '.implode('/', $wanted).' but got an empty response'));
}
What that tells me is that you received no response from the mail server.
So once again I recommend checking the output of env from the app directory. If your environment variables are not reflected, try updating them and running:
php artisan cache:clear
php artisan config:clear
And run env once again to check if the updates are reflected.
Currently I have started learning about Unit Testing in Laravel 5.6.
By default my laravel project has a 'tests' directory inside which I have 2 more directories namely, 'Features' and 'Unit'. Each of these directories contain a 'ExampleTest.php'
./tests/Features/ExampleTest.php
./tests/Unit/ExampleTest.php
Whenever I create new test file using command
php artisan make:test BasicTest
It always creates the test file inside the 'Features' directory by default, where as I want the file to be created under the 'tests' directory.
Is there a command using which I can specify the path fro creation of the test file.
Something like this
php artisan make:test BasicTest --path="tests"
I have already tried the above path command but it is not a valid command.
Do I need to change some code in my phpunit.xml file?
php artisan make:test Web/StatementPolicies/StatementPolicyListTest
It will by default create a file namely StatementPolicyListTest under StatementPolicies(if not exist it will create a new folder of this name) folder under tests/Feature/Web
Use this command
php artisan make:test BasicTest --unit
Also you can use
php artisan make:test --help
to see available options
You must be create your custom artiasn command
<?php
namespace App\Console;
class TestMakeCommand extends \Illuminate\Foundation\Console\TestMakeCommand
{
/**
* The console command name.
*
* #var string
*/
protected $signature = 'make:test-custom {name : The name of the class} {--unit : Create a unit test} {--path= : Create a test in path}';
/**
* Get the default namespace for the class.
*
* #param string $rootNamespace
* #return string
*/
protected function getDefaultNamespace($rootNamespace)
{
$path = $this->option('path');
if (!is_null($path)) {
if ($path) {
return $rootNamespace. '\\' . $path;
}
return $rootNamespace;
}
if ($this->option('unit')) {
return $rootNamespace.'\Unit';
}
return $rootNamespace.'\Feature';
}
}
Register it in kernel
<?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 = [
TestMakeCommand::class
];
......
}
Then you can use
php artisan make:test-custom BasicTest --path=
or
php artisan make:test-custom BasicTest --path=Example
Few weeks ago, I had the same problem in Laravel 5.1, which I could solve with this solution.
However, now I'm facing the same issue in Lumen, but I can't call php artisan view:clear to clear the cached files. There is any other way?
Thanks!
There's no command for the view cache in lumen, but you can easily create your own or use my mini package found at the end of the answer.
First, put this file inside your app/Console/Commands folder (make sure to change the namespace if your app has a different than App):
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class ClearViewCache extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $name = 'view:clear';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Clear all compiled view files.';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$cachedViews = storage_path('/framework/views/');
$files = glob($cachedViews.'*');
foreach($files as $file) {
if(is_file($file)) {
#unlink($file);
}
}
}
}
Then open app/Console/Kernel.php and put the command inside the $commands array (again, mind the namespace):
protected $commands = [
'App\Console\Commands\ClearViewCache'
];
You can verify that everything worked by running
php artisan
inside the project's root.
You will now see the newly created command:
You can now run it like you did in laravel.
EDIT
I've created a small (MIT) package for this, you can require it with composer:
composer require baao/clear-view-cache
then add
$app->register('Baao\ClearViewCache\ClearViewCacheServiceProvider');
to bootsrap/app.php and run it with
php artisan view:clear