To create a custom ddev command for running php bin/magento in a Magento project, follow these steps:
1. Open the .ddev/commands/web or .ddev/commands/host directory
This is where custom commands for ddev are stored.
2. Create a new file for the command
Create a file named .ddev/commands/web/magento (no file extension) and add the following content:
#!/bin/bash
# Runs php bin/magento inside the web container
php bin/magento "$@"
3. Make the file executable
Run the following command to give execution permissions:
chmod +x .ddev/commands/web/magento
4. Apply the changes
Restart your ddev project to load the new command:
ddev restart
5. Use the command
You can now run any Magento command using ddev:
ddev magento cache:flush
ddev magento setup:upgrade
Explanation
"$@"ensures all arguments passed afterddev magentoare forwarded tophp bin/magento.- Placing the script in
.ddev/commands/web/makes it available inside the container, while placing it in.ddev/commands/host/makes it available on your local machine.
And that's it! 🎉 Now you can easily run Magento commands with ddev.
Comments