Symfony - can't run created console command - php

I've created a file called GenerateRandomUserCommand.php inside vendor/symfony/console/Command. Its namespace is set to: Symfony\Component\Console\Command it extends Command aswell. Clean composer and Symfony installation. The problem is, I can't make command working. I write: php bin/console mycommand (it is set of course in class) and I have a message: Command "mycommand" is not defined.
What's the problem? I did nothing in kernel or config, everything is default.

It's need to see that not working command file. But I can suggest to you use symfony maker bundle.
First, require bundle for dev env: composer require maker --dev
Then simply type in console bin/console make:command. After typing name it was generate command skeleton with some simple example. And I beleive that's all you need =)

The problem was, I must register command. Despite of autoregistering provided by Symfony...

Related

How to run a custom command create in composer.json with options?

I'd like to use the Codeception bin file in project A from project B, so in project B's composer.json file I have added this script:
"codecept": "../projectA/vendor/bin/codecept"
With this, I can run a command from project B such as:
composer codecept bootstrap
However, if I add an option that can be used with the bootstrap command, such as:
composer codecept bootstrap --empty
Composer throws with the following error:
[Symfony\Component\Console\Exception\RuntimeException]
The "--empty" option does not exist.
This is my first time playing around with custom commands, so I'm not sure what I'm missing.
Okay, I found it, have to use an extra -- before passing the option, like this:
composer codecept bootstrap -- --empty
The error you see is from Symfony Console that is a dependency of Composer.
The underlying problem is that the command-line argument parser in Symfony Console is flawed (read: limited).
You can work-around that in many cases - but not for all - by using the -- argument delimiter and adding the commands arguments behind. E.g.:
composer codecept bootstrap -- --empty
However this does only work for command-line options that do not exist in Symfony Console as global arguments. E.g. this won't work for --help, it will show Composers' help text. Or --version, or --quiet and so on and so forth, e.g.:
composer exec codecept-the-foo-baloo bootstrap -- --version
Composer version ...
The command/package must not even be installed. As written this is flawed and introduces such and similar kind of limitation(s).

Symfony there are no commands defined in the "make" namespace

Following the documentation found here I enter php bin/console make:entity Product in Terminal and get the following error:
[Symfony\Component\Console\Exception\CommandNotFoundException]
There are no commands defined in the "make" namespace.
Maybe you were using the prod environment?
The website-skeleton puts the Maker Bundle in the require-dev section of your composer.json by default:
"require-dev": {
...
"symfony/maker-bundle": "^1.0",
...
}
If you've set APP_ENV=prod in your .env file and ran bin/console it would ignore all dev requirements and thus wouldn't enable the Maker Bundle.
Simply enabling the dev environment again (APP_ENV=dev) would do the trick then.
make is a command of doctrine component. Just add doctrine maker.
composer require doctrine maker
https://symfony.com/doc/current/doctrine.html#installing-doctrine
You need Symfony 3.4 or higher. For Symfony 3.4 you will need to add it to the registerBundles function in config/AppKernerl():
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
// [...]
$bundles[] = new \Symfony\Bundle\MakerBundle\MakerBundle();
}
Bear in mind that the environment where it is installed is 'dev'.
try
composer remove maker
composer require maker --dev
and then
php bin/console make:entity Product
https://symfony.com/doc/current/bundles/SymfonyMakerBundle/index.html
You must define the "dev" env in the command line :
php bin/console --env dev
php bin/console make:entity Product --env dev
The problem just happend to me.
I was in test environment.
You have to be in dev environment.
Check APP_ENV value in .env file. It must be the following :
APP_ENV=dev
I think you are in prod environment. Check if your the value of APP_ENV inside .env file is not equals to prod.
In case you generate a .env.local file, you have to delete it in your dev environment.
You can use composer for installer maker bundle, you have install a light version of symfony.
composer require symfony/maker-bundle
but if you have this problem surely need composer doctrine and security
composer require doctrine/orm
with
require doctrine/doctrine-bundle
And
composer require symfony/security-bundle
composer require doctrine/doctrine-migrations-bundle
Now you have this with php bin/console !
make make:auth Creates a Guard authenticator of
different flavors
make:command Creates a new console command class
make:controller Creates a new controller class
make:crud Creates CRUD for Doctrine entity class
make:docker:database Adds a database container to your
docker-compose.yaml file
make:entity Creates or updates a Doctrine entity
class, and optionally an API Platform resource
make:fixtures Creates a new class to load Doctrine
fixtures
make:form Creates a new form class
make:message Creates a new message and handler
make:messenger-middleware Creates a new messenger middleware
make:migration Creates a new migration based on
database changes
make:registration-form Creates a new registration form system
make:reset-password Create controller, entity, and
repositories for use with symfonycasts/reset-password-bundle
make:serializer:encoder Creates a new serializer encoder class
make:serializer:normalizer Creates a new serializer normalizer
class
make:stimulus-controller Creates a new Stimulus controller
make:subscriber Creates a new event subscriber class
make:test [make:unit-test|make:functional-test]
Creates a new test class
make:twig-extension Creates a new Twig extension class
make:user Creates a new security user class
make:validator Creates a new validator and constraint
class
make:voter Creates a new security voter class
If you have installed symfony / maker-bundle for the dev mode you will only be able to use it for this mode. If you are in
prod mode then go to dev mode and try again
Try composer require symfony/maker-bundle --dev it work for me.
composer require symfony/maker-bundle --dev
To fix the problem you just create new project with (full properties), here is command for create a project:
symfony new --full project-name
after you create project like the command above, you now can able to use (make).

The Symfone "doctrine:fixtures:load" command missing

I have Symfony 3.1 project with Doctrine and
doctrine/doctrine-fixtures-bundle and
doctrine/data-fixtures installed.
However, when i run a console command
php bin/console doctrine:fixtures:load
i get a message saying that there are no commands defined in the doctrine:fixtures namespace.
The class Doctrine\Bundle\FixturesBundle\Command\LoadDataFixturesDoctrineCommand exists.
Please suggest how to fix that.
Run first php bin/console and see if the command appear in the list.
If it doesn't, then you have to load the bundle in the AppKernel.php
$bundles[] = new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle();
php bin/console default environment is PROD
and fixtures proper installation is require-dev
so proper function call is:
php bin/console --env=dev doctrine:fixtures:load
Also rm -rf var/cache worked for me (after the bundle registration)
Data Fixtures Mechanism requires a special bundle. It is not included in Sf by default.
Check: http://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html
and install it.
Bundle called DoctrineFixturesBundle. You can also find there many useful information how to use fixtures.
So that why you've got message "Command is not defined".

vendor/bin/phpunit not working for Lumen

I'm new to Lumen (and Laravel). I've created a project with Composer and now I'm trying to get PHPUnit to work.
I'm following a book, where it should be possible to run a default passing test by typing vendor/bin/phpunit in the terminal, but it gives the error:
'vendor' is not recognized as an internal or external command,
operable program or batch file
I've checked that the phpunit file is actually there and that phpunit is added as dependency in my composer.json file. I've also tried ./vendor/bin/phpunit and vendor/bin/phpunit/phpunit, but with the same result.
I've searched Google to find a solution, but everyone else seem to have issues when running phpunit (wihout vendor/bin) and the solution is to use the full path vendor/bin/phpunit, but since I'm already doing that, it does not fix my problem.
I'm using PHPStorm on a Windows machine and running the PHP server via PHPStorm. I've not modified the default Lumen project.
Any help is greatly appreciated!
UPDATE:
Trying php vendor/bin/phpunit gives the following error:
You need to set up the project dependencies using the following
commands:
wget http://getcomposer.org/composer.phar
php composer.phar install
I'm not sure what that means, since I've already installed Composer. I used Composer to create the project and I haven't changed the dependencies from the default.
I had the same problem, for Windows it's vendor\bin\phpunit ;)
It turned out that some symlinks and permissions were not installed properly in the default project. I tried deleting the entire vendor/ directory and run composer install.
Now I can run phpunit with the command vendor\bin\phpunit (because I'm running on Windows - thanks Nizarii)
try this:
php vendor/bin/phpunit
Try putting php in front of the phpunit path like so:
php vendor/bin/phpunit

symfony2 tutorial - generate bundle

I am new to symfony2 but not to symfony. I am currently doing the symblog tutorial. Its going ok so far however I have come a little stuck on the generate bundle?
The tutorial says to run the following command
php app/console generate:bundle --namespace=Blogger/BlogBundle --format=yml
After completing that it should add reference/generate code to the bundle in the
app/AppKernel.php
app/config/routing.yml
However it has not added anything?... I'm a bit confused? The console outputs the following but the code references to the bundle have not been generated in the files
Welcome to the Symfony2 bundle generator
Your application code must be written in bundles. This command helps
you generate them easily.
Each bundle is hosted under a namespace (like Acme/Bundle/BlogBundle).
The namespace should begin with a "vendor" name like your company name, your
project name, or your client name, followed by one or more optional category
sub-namespaces, and it should end with the bundle name itself
(which must have Bundle as a suffix).
See http://symfony.com/doc/current/cookbook/bundles/best_practices.html#index-1 for more
details on bundle naming conventions.
Use / instead of \ for the namespace delimiter to avoid any problem.
Bundle namespace [Blogger/BlogBundle]:
You can use this without interaction . then it won't ask you anything
php app/console generate:bundle --namespace=Blogger/BlogBundle --format=yml --no-interaction
From generate:bundle --help
If you want to disable any user interaction, use --no-interaction but don't forget to pass all needed options:
php app/console generate:bundle --namespace=Acme/BlogBundle --dir=src [--bundle-name=...] --no-interaction
I have bash functions for these long commands.
genbundle () {
php app/console generate:bundle --namespace=$1/$2Bundle --bundle-name=$2Bundle --dir=src/ --format=yml
}
You can use it like this: "genbundle Acme Blog", It'll create a BlogBundle in the Acme app.
If I understood what's going on correctly, you have to go on the process. It's step-by-step where you answer the questions. Keep answering everything asked, and then the console will tell when it created the bundle.
Note: the value in [...] are the default value if you just press enter when the console asks you something.
Old Command:
php app/console generate:bundle --namespace=Blogger/BlogBundle --format=yml --no-interaction
In new symfony version 3.1.4, "app/console" is no more working.
Instead add "bin/console" in above command
So the New command will be
php bin/console generate:bundle --namespace=Blogger/BlogBundle --format=yml --no-interaction

Categories