Classes, Namespacing, bin/console commands - php

I just started with Symfony framework this afternoon. I moved onto "CRUD" and was following a tutorial to use the tools built in to generate the CRUD. However, using the default commands, I am getting errors.
Attempted to load class "Task" from namespace "AppBundle\Controller".
Did you forget a "use" statement for another namespace?
500 Internal Server Error - ClassNotFoundException
This is the video I was following.
I started with a blank installation of Symfony.
Connected the database.
Ran php bin/console generate:doctrine:entity, wrote
AppBundle:Tasks, and pressed enter through all options defaults to
create a simple name and notes field.
Ran php bin/console generate:doctrine:crud wrote AppBundle:Tasks, and pressed enter through all option defaults but selecting yes for generating all the CRUD commands
Ran php bin/console doctrine:schema:update --force
Ran php bin/console cache:clear
Go to //localhost/symfony/web/app_dev.php/tasks and I'm brought to the list, works fine.
But if I go to localhost/symfony/web/app_dev.php/tasks/new or click the generated button, I get the error mentioned above.
The error message is greek to me, despite its best attempts to be polite. I can see there is a Tasks.php file under Entity and it defines the class "Tasks" and the TasksController is set to use AppBundle\Entity\Tasks so I have no idea why it is not working with my limited understanding of all this.
I'm trying to build a fairly simple Task Management app but am completely new to Symfony and do not have a solid background in PHP to begin with. Could someone please explain where in the process I may have gone awry?

Things to check:
The name of the file Task.php is the same than the class name class Task. Note: a class should not be in plurial, so Task is right et Tasks is wrong
You have a use AppBundle\Entity\Task; between your Controller namespace and class declaration
To avoid this error, you could use a PHP IDE like Phpstorm (or other), it display your statement in color when you forget to add a use statement.It also add it automatically :D

Related

is php artisan make::controller required for Laravel development?

I'm starting out learning Laravel, and all the tutorials show how to use
php artisan make:controller BlahController
to make a new controller.
Is there a requirement I do this from the command line? If I want to do it by hand, what would I need to do to replicate that action?
I know that of course, I'd need to manually create the BlahController.php file inside the app\http\Controllers folder.
But does artisan make:controller alter/create any other files?
Broader question. Is php artisan required at all to work in Laravel? Or can all development be done by hand coding?
No, it is not required at all to use php artisan to create any files.
The purpose of artisan is to make your work more automated or less coding in general, but you are 100% correct, you can just manually create the controller file on its own (or model or view or anything related to laravel) without using artisan.
Example of artisan is to do things like:
php artisan create:model MyModel -all
This will create:
migration
seeder
factory
policy
resource controller
form request classes for the model
all of that is created and ready to be edited in a single command rather than having to manually create each file and filling it up and checking if the names are correct etc...
You can do so many things with artisan to simplify the process, and you can always read more on what options you have when creating things by doing:
php artisan create:controller -help
More of the documentation can be found here: https://laravel.com/docs/9.x/artisan#introduction
Laravel artisan helps you to do things faster
Ofcourse you can create it manually but artisan makes your life a little bit easier!
I highly recommend it
Also It's not about just creating a controller
It does a lot, read more about it here:
https://laravel.com/docs/9.x/artisan

How to load tables with static content in production environment with symfony 3.4

Scenario
We want to autofill a table with new static content with doctrine, ideally with something like a fixture class or similar.
We follow the simple development life-cycle with development to staging to production. And we are using Doctrine v2.6 with Symfony v3.4. Every release step is executed by an Jenkins job.
For development and staging we use the very useful and simple doctrine-fixtures-bundle to auto-fill our database with test-datasets. The database-schema is auto-generated by doctrine:schema:update on basis of our entities.
I've tried to use the fixtures also for production but even with doctrine:fixtures:load --fixtures=src/MyBundle/DataFixture/ORM/MyFixture.php it is purging the whole database. Then I read something about the --append command to prevent doctrine from purging the database. But then it will append the datasets in every release process (?). Nevertheless, it also feels like a very bad practice.
What I'm wondering
Is it possible to truncate the table, load table records with static data loaded from a class that can be executed via a command line? Or is there a completely different (and clean) way for such a case? Is the doctrine:migration bundle the real way to go?
Thanks for your help!
You should create a Command for populate tables.
https://symfony.com/doc/current/console.html

Typo3 scheduler: Can i somehow execute Action of controllers of my extension with it? Or how to run my own code with it?

A while ago i was tasked to program a Typo3 extension to write so called .conf files for the icinga2 montoring tool (has nothing to do with Typo3). Still let me explain some parts of it: Basically the backend user needs to create records of records of specific classes and set values for each records properties. Then i need to process the records to create these .conf files with the specific values with a php script.
I was tasked to use the scheduler in Typo3 for this. And here come the problems: How do i use this? I checked the documentation (https://docs.typo3.org/typo3cms/extensions/scheduler/Introduction/Index.html), but i still can't wrap my head around how to use it for my task. I can easily write an Action in a controller of a class to be executed in the frontend and in turn generate the con files... basically manually without the scheduler. But where do i put my php code to be run by the scheduler? I somehow seem not to understand the basical principle of the scheduler. Can i just run an Action of a specific controller of a class of my extension like i would in the Frontend via the scheduler?
I would suggest you use a command controller for this task.
The documentation shows how to create a command controller, which also may accept arguments.
Command controller tasks can directly be executed by TYPO3 scheduler (see screenshot below)
You may even configure task arguments for command controller tasks in TYPO3 scheduler.

Lithium: New command don't work

I've acted in accordance with this guid Link.
Then, I created Repos.php file in /app/extensions/command/ And run li3 in command line but do not see the following message:
COMMANDS via app
repos
I see the following text only:
Lithium console started in the development environment. Use the --env=environment key to alter this.
COMMANDS via lithium
create
The `create` command allows you to rapidly develop your models, views, controllers, and tests
by generating the minimum code necessary to test and run your application.
g11n
The `G11n` set of commands deals with the extraction and merging of message templates.
help
Get information about a particular class including methods, properties,
and descriptions.
route
The route command lets you inspect your routes and issue requests against the router.
test
Runs a given set of tests and outputs the results.
See `li3 help COMMAND` for more information on a specific command.
Have I done something wrong?

Symfony2 console doesn't interact

When I was creating projects in Symfony2 on Windows there were no problems, but now (linux) I got something like this:
when I want to generate bundle without any parameters I get
[RuntimeException]
The "namespace" option must be provided.
When I type in the namespace I get:
[RuntimeException]
The "dir" option must be provided.
I can create bundle only if I give all the necessary arguments at the beginning and I'm kinda lazy person and it's quite annoying for me that console doesn't ask about namespace or anything. Is there any chance to repair this?:P
EDIT: actually this happens whenever I try to run function which requires arguments.
I've tried too to create a bundle with Netbeans or Eclipse but i have encountered the same problem.
The unique solution is to create your bundle with the Terminal.
All the process will run normally, and you'll only to answer the questions for finish your creation.
I'll come back to you if i've other solutions.
Cordially.
your problem doesn't come from netbeans, I use phpstorm and I had the same problem.
later I found out that the problem comes from symfony to solve it go to your project folder for me it was like:
/opt/lampp/htdocs/MyProject/vendor/symfony/symfony/src/Symfony/Component/console and open file Application.php in a text editor
search for
$input->setInteractive(false);
then comment it like this :
//$input->setInteractive(false);
it should perfectly work now !

Categories