Sail - Initiate Xdebug session from command line - laravel

So the last version of Sails makes it very easy to use Xdebug. Basically just had to define SAIL_XDEBUG_MODE in the .env file, configure path mapping in PhpStorm, activate listening and all was set - it works perfectly from the browser.
Now, how should I go if I want Xdebug to activate when I'm using the command line? Like when using artisan commands for seeding, or even better when using custom artisan commands created to run scripts to update some data... I can't find any arguments to add to my sail artisan myOwnCommand that would tell Xdebug it has to activate.
I'm working on Windows 11 with WSL2.
Thanks ahead!

Thanks to Derick's suggestion, I found out a solution. Prepending a sail call with anything wouldn't help, since sail calls scripts in the docker container, and your environment variable wouldn't be set there. But since it was just about setting an environment variable, it can be easily done in docker-compose file.
I just had to add PHP_IDE_CONFIG: 'serverName=0.0.0.0' in the environment section of my Laravel service. Of course replace 0.0.0.0 with your own server name. Then, instead of running sail artisan test or sail artisan my:command you replace artisan with debug, as stated in the docs.

Now you can use command debug to run with Xdebug (ex. sail debug myOwnCommand).
Here is documentation: https://laravel.com/docs/9.x/sail#xdebug-cli-usage

Related

Vite network URL opens Vite development server in Laravel application

When I run vite run dev --host in my Laravel project, I get below screen on my external network URL:
My external URL is: http://192.168.1.6:5173
➜ Local: http://localhost:5173/
➜ Network: http://192.168.1.6:5173/
Project runs fine on my app URL defined in Laravel's .env file viz. http://trip.dev
I am expecting http://192.168.1.6:5173/ to work same as http://trip.dev
I tried php artisan serve (as suggessted in the above screen to run the local development server), but it keep showing me the same screen.
Can some one help me in this?
You should run php artisan serve --port=8000 in a command prompt and run vite run dev --host or npm run dev in other command prompt.
now, you need to open http://YOUR_IP_IN_NETWORK:8000; for example if your ip is 192.168.1.6, you should open http://192.168.1.6:8000.
tip:
Make sure you use #vite('...path...') in your code to use vite.

Laravel Serve command does not respect --env parameter

To be able to run Browser tests directly in my IDE (without using the artisan dusk command), I want to run php artisan serve --env=dusk.local. While it indeed starts the local PHP server, it uses the wrong database. It uses the database specified in .env not the one in .env.dusk.local.
I ran php artisan cache:clear thousands of times, but it doesn't change anything.
Running things like php artisan migrate --env=... works.
Is there a way to achieve my goal without needing to rename my .env.dusk.local file to .env before each test?
This is a bug in Laravel 5.8: https://github.com/laravel/framework/issues/27828
There is currently no solution (other than downgrading to Laravel 5.7).
It has been fixed in the latest release 5.8.7.

Xdebug of Laravel Dusk in Netbeans on Homestead

In Laravel Homestead, I've been able to use Xdebug for unit tests, feature tests, poking around in the browser, etc.
But it hangs when I try to use Xdebug for Dusk (tests in tests/Browser folder).
I thought these questions might help, but I still haven't gotten it working:
debugging laravel artisan from PHPStorm with homestead
Xdebug laravel artisan commands
I've tried various approaches, including:
export XDEBUG_CONFIG="idekey=netbeans-xdebug remote_connect_back=0 remote_host=10.0.2.2"
php artisan dusk
and
export XDEBUG_CONFIG="idekey=netbeans-xdebug remote_host=10.0.2.2"
php -dxdebug.remote_autostart=on -dxdebug.remote_connect_back=off -dxdebug.remote_host=10.0.2.2 artisan dusk
and more.
I've enabled the "Stop at First Line" debugging option in Netbeans, and Netbeans does successfully stop at the first executable PHP line in the artisan file.
Therefore I think export XDEBUG_CONFIG="idekey=netbeans-xdebug remote_connect_back=0 remote_host=10.0.2.2" is correctly set up.
But after I click the "play" button to allow the code to continue, Netbeans just says "netbeans-xdebug running" in the bottom right while the console just hangs with a cursor flashing under this line: php artisan dusk tests/Browser/ExampleTest.php
How do I need to change my usage of Xdebug for getting it to work in Dusk too?
It doesn't work because Dusk executes the actual PHPUnit test in a separate process, so it doesn't know about XDEBUG_CONFIG.
In principle, Dusk tests still work when you execute them directly (phpunit tests/Browser/ExampleTest.php). The main feature of php artisan dusk are custom .env.dusk[.local] files.
If you don't require that, you can try calling them directly. Then Xdebug should behave the same way it does with all your other PHPUnit tests.
As in my answer here, there is an option of running php -dxdebug.remote_enable=1 -dxdebug.remote_host=10.0.2.2 -dxdebug.remote_port=9000 -dxdebug.remote_handler=dbgp artisan my:command
You can also add those parameters to xdebug.ini like so:
zend_extension_ts = "./php/ext/php_xdebug<-version-number>.dll"
xdebug.remote_enable=1
xdebug.remote_host=10.0.2.2
; Port number must match debugger port number in NetBeans IDE Tools > Options > PHP
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
xdebug.idekey=netbeans-xdebug (although I think it should work without this too)
Try removing break at first line option. Also try running debug directly from console with those options. And finally check that the correct port is set to listen in NetBeans and xdebug since that could cause problems too.

Laravel-Php artisan serve url (127.0.0.1:8000) vs localhost/laravelproject/public

I want to access my laravel project.I run php artisan serve and access the 127.0.0.1:8000 in browser.
But i learned that I can also check my project using the localhost/laravelproject/public url whithout running php artisan serve.
Question: What is the point of using php artisan serve?
No point in two different methods like you mentioned run laravel by "php artisan serve" and by "project url" followed by localhost. But advantage of "php artisan serve" is you can run you laravel project without putting in htdocs/www directory i.e servers root directory. You can put laravel project anywhere where you want and run through the artisan command.
I found some information you may find interesting:
https://www.quora.com/How-can-I-use-php-artisan-serve
But in simple words, php artisan serve is an easy way to create a php server and that is what laravel needs to run.
You could do the same with "php -S 8080 (which would start a php web server (single threaded) in the current directory on port 8080)"
Also if you have already a php server running with apache or nginx it would not be necessary any of the commands.
Hope you find this helpful.
The `Serve command is just a shortcut for the PHP Builtin Webserver, something PHP has out of the box, so the point of using it is to start testing your application as fast as you could, you just need to install PHP, Composer and your application is up (if you don't need anything else, of course). But if you already have Nginx installed, there is no point at all, just use it.
It's not wise to use the Builtin Webserver in production.

Laravel does not read env variables

Did anyone has issues with env variables? For some reason, helper env('VARIABLE') returns null every time I use it. It happened very unexpectedly and I dont really know the reason. Restarting the apache/IDE/computer does not work.
Run those of command
composer dump-autoload
php artisan cache:clear
php artisan config:clear
php artisan view:clear
Now try to read
$value = env('VARIABLE_NAME');
if not working till now then,
Try another syntax to read env variable.
$value=getenv('VARIABLE_NAME');
The solution is simple, but neither the IDE nor the debugger says anything about it. It just returns null. When you use php artisan config:cache, according to the documentation:
If you execute php artisan config:cachecommand during your deployment process, you should be sure that you are only calling the env() function from within your configuration files.
Obviously I have env variables outside the config files, so after caching I was not able to use it outside anymore. The php artisan config:clear puts it back to work.
What I've found more about the usage of env, that it should be used only within config files. You can access env variables from the rest of the project using other helper method config(). Be sure to assign it to another key in config file, e.g. 'key' => env('CACHE_DRIVER')
What is more, you have to remember to run php artisan config:cache each time you will change .env file. Laravel will not load the new values, until it's cached. If it's not cached, no need to do it.
please use this its work for me use git bash or cmd and past this command
$ rm bootstrap/cache/config.php
this command clear cache folder
Just run
php artisan config:clear
It solved my issue.
In my case, it's just because I'm using php artisan serve, just restart it, the server command may read the env and keep at the startup time.

Resources