TYPO3 Symfony Services.yaml class not find - symfony

I am trying to update an older extension to Typo3 V11.
Currently I am got the following error:
Here ma Services.yaml file:
# Configuration/Services.yaml
services:
_defaults:
autowire: true
autoconfigure: true
public: false
HGA\Simpleblog\:
resource: '../Classes/*'
exclude: '../Classes/Domain/Model/*'
HGA\Simpleblog\Utilities\SqlUtil:
public: true
And here the beginnig of the BlogsController.php file:
<?php
namespace HGA\Simpleblog\Controller;
use \Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Http\ForwardResponse;
use TYPO3\CMS\Core\Core\Environment as Environment;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use HGA\Simpleblog\Utilities;
use HGA\Simpleblog\Utilities\SqlUtil;
use HGA\Simpleblog\Domain\Model;
use HGA\Simpleblog\Domain\Repository;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
use TYPO3\CMS\Extbase\Annotation\Inject;
/***
*
* This file is part of the "SimpleBlog" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
***/
/**
* BlogsController
*/
class BlogsController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
{
/**
* #var SqlUtil $sqlUtil
*/
protected $sqlUtil;
public function __construct(SqlUtil $sqlUtil)
{
$this->sqlUtil = $sqlUtil;
}
In my opinion, the class BlogsController is included into the mentioned file.
The extension was working with an older version of Typo3. But I had to add the Services.yaml file.
I am not using composer!
TYPO3 V11.5.8.
Any idea, what could be the problem?
EDIT
After a while, the problem disappeared! What I did in between, I had to updated the Windows Server 2019 operation system.

Does the filename is really BlogController.php but should be BlogsController.php.

Related

Symfony 3.4 NotFoundHttpException No route found for "GET /lucky/number"

I'v created a symfony 3.4 project,and I've create a controller following the doc (I had to change the controller namespace)
<?php
namespace App\Controller;
//namespace AppBundle\Controller; this is the default namespace in the doc
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class LuckyController
{
/**
* #Route("/lucky/number")
*/
public function numberAction()
{
$number = random_int(0, 100);
return new Response(
'<html><body>Lucky number: '.$number.'</body></html>'
);
}
}
But when I try to access to http://127.0.0.1:8000/lucky/number I get this error:
NotFoundHttpException No route found for "GET /lucky/number"
I tried to clean the cache but didn't work and think I dont have to download any anootations library, I don't know what is wrong
If you changed the namespace you also need to make some changes to your config.
You can tag the individual controller with controller.service_arguments:
# app/config/services.yml
services:
# ...
# explicitly configure the service
App\Controller\LuckyController:
tags: [controller.service_arguments]
Or if you have changed the entire AppBundle namespace to App and you are using the Symfony Standard Edition (version 3.4) services.yml configuration, just change all instances of AppBundle to App:
# app/config/services.yml
services:
# default configuration for services in *this* file
_defaults:
autowire: true
autoconfigure: true
public: false
# makes classes in src/App available to be used as services
App\:
resource: '../../src/App/*'
# you can exclude directories or files
# but if a service is unused, it's removed anyway
exclude: '../../src/App/{Entity,Repository}'
# controllers are imported separately to make sure they're public
# and have a tag that allows actions to type-hint services
App\Controller\:
resource: '../../src/App/Controller'
public: true
tags: ['controller.service_arguments']
Of course always clear the cache after making any changes.

Symfony service FileUploader not autowiring

I've followed the Symfony 5.2 tutorial to add a FileUploader as a service (https://symfony.com/doc/current/controller/upload_file.html).
So this is my service.yaml
parameters:
targetDirectory: '%kernel.project_dir%/public/uploads/'
previews_video: '%kernel.project_dir%/public/uploads/previews'
brochures_directory: '%kernel.project_dir%/public/uploads/brochures'
services:
App\Service\FileUploader:
arguments:
$targetDirectory: '%previews_video%'
And this is my FileUploader.php
<?php
namespace App\Service;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\String\Slugger\SluggerInterface;
class FileUploader
{
private $targetDirectory;
private $slugger;
public function __construct($targetDirectory, SluggerInterface $slugger)
{
$this->targetDirectory = $targetDirectory;
$this->slugger = $slugger;
}
public function upload(UploadedFile $file)
{
$originalFilename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
$safeFilename = $this->slugger->slug($originalFilename);
$fileName = $safeFilename.'-'.uniqid().'.'.$file->guessExtension();
try {
$file->move($this->getTargetDirectory(), $fileName);
} catch (FileException $e) {
// ... handle exception if something happens during file upload
}
return $fileName;
}
public function getTargetDirectory()
{
return $this->targetDirectory;
}
}
But I'm having this common error :
Cannot resolve argument $fileUploader of "App\Controller\VideoController::edit()": Cannot autowire service "App\Service\FileUploader": argument "$targetDirectory" of method "__construct()" has no type-hint, you should configure its value explicitly.
Called by this controller :
/**
* #Route("/{id}/edit", name="video_edit", methods={"GET","POST"})
* #param Request $request
* #param Video $video
* #param FileUploader $fileUploader
* #return Response
*/
public function edit(Request $request, Video $video, FileUploader $fileUploader): Response
{...}
How do I fix this ? I trying by remove the string type, adding the string type, removing the $ from the targetDirectory parameters in services.yaml... Struggling with that for hours now...
Take a look at my working services.yaml. I've changed the namespace
App\Service
to
App\Services
And I also added the service declaration at the end of the file.
Looks like the order of the lines in services matter. First, I've added the declaration at the top of the services part, but the autowiring is declared after, guess the error was here...
# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
previews_directory: '%kernel.project_dir%/public/uploads/previews'
services:
#i've added my service here at first...
app.menu_builder:
class: App\Menu\MenuBuilder
arguments: ["#knp_menu.factory"]
tags:
- { name: knp_menu.menu_builder, method: createMainMenu, alias: main }
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
- '../src/Tests/'
# controllers are imported separately to make sure services can be injected
# as action arguments even if you don't extend any base controller class
App\Controller\:
resource: '../src/Controller/'
tags: ['controller.service_arguments']
# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones
App\Services\FileUploader:
arguments:
$targetDirectory: '%previews_directory%'
You should have autowiring configuration added to your services file:
parameters:
targetDirectory: '%kernel.project_dir%/public/uploads/'
previews_video: '%kernel.project_dir%/public/uploads/previews'
brochures_directory: '%kernel.project_dir%/public/uploads/brochures'
services:
# To add:
_defaults:
autowire: true
autoconfigure: true
# You service
App\Service\FileUploader:
arguments:
$targetDirectory: '%previews_video%'
Add a type-hint string for $targetDirectory in the contructor
public function __construct(string $targetDirectory, SluggerInterface $slugger)
{
$this->targetDirectory = $targetDirectory;
$this->slugger = $slugger;
}
I had the same issue.
It was related to the indentation of that specific service. I wasn't getting any indentation error but also the auto wiring wasn't working.
What i did was to add 4 spaces as indentation
App\Service\FileUploader:
arguments:
$targetDirectory: '%TEAM_LOGO_DIRECTORY%'
Yes I got the problem too and I managed to solve it by replacing indentation by spaces in the services.yaml file, I added all of these properties at the same root and then I did that and it works for me:
services:
App\Services\FileUploader: #(added 4 spaces, NOT 1 tab)
arguments: #(added 8 spaces, NOT 2 tabs)
$targetDirectory: '%cats_directory%' #(added 12 spaces, NOT 3 tabs)
If you struggle you (and other people who got this problem) can try this solution. I don't guarantee it will work 100%.

(Symfony 4) Unable to inject a github library that I installed with composer

I installed the following library from GitHub: https://github.com/rosell-dk/webp-convert
The location of the main class that I need to is (from project root) :
\vendor\rosell-dk\webp-convert\src\WebPConvert.php
Here is how the WebPConvert.php class starts out:
namespace WebPConvert;
use WebPConvert\Converters\ConverterHelper;
use WebPConvert\ServeExistingOrConvert;
use WebPConvert\Serve\ServeExistingOrHandOver;
class WebPConvert
{
In the repository class where I use it, here is how I tried to do my dependency injection:
use WebPConvert\WebPConvert;
class PhotoRepository extends ServiceEntityRepository
{
/**
* #var WebPConvert
*/
protected $webPConverter;
public function __construct(WebPConvert $webPConverter)
{
$this->webPConverter = $webPConverter;
}
I followed the instructions from https://symfony.com/doc/current/service_container.html
But I still get this message:
Cannot autowire service "App\Repository\PhotoRepository": argument "$webPConverter" of method "__construct()" references class "WebPConvert\WebPConvert" but no such service exists.
I have even tried putting this in my services.yaml and it doesn't work:
App\Repository\PhotoRepository:
arguments:
- WebPConvert\WebPConvert
Is there an extra step I am missing?
This is Cerad's answer which worked:
WebPConvert is not a Symfony bundle so it won't have any services defined. You will have to define them manually. Actually, from the readme file, it looks like WebPConvert::convert is a static method so there is nothing to inject. Just follow the example.

"The controller must return a response" while using #Template annotation

Using Symfony Framework:
"sensio/framework-extra-bundle": "^5.1"
+
"symfony/framework-bundle": "^4.1"
with default configuration.
Receive error: The controller must return a response (Array() given).
Sample code:
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
/**
* #Route("/")
*/
class IndexController extends Controller
{
/**
* #Route("", name="index")
* #Template()
*/
public function index()
{
return [];
}
}
I tried to add:
sensio_framework_extra:
view:
annotations: true
But it doesn't work
I have same sample project with "symfony/framework-bundle": "^4.0" and it work properly.
It turns out I created a new project from scratch, but did not use the symfony/website-skeleton package that would normally install all dependencies. So it wasn't just that my #Template annotation wasn't working, it was that no templates were working because Twig wasn't installed.
I ran the command composer require twig-bundle and it solved the problem.
Take a look at the #Template annotation documentation. It states:
As of version 4.0 of the bundle, only Twig is supported by the #Template annotation (and only when not used with the Symfony Templating component -- no templating entry set in the framework configuration settings).
I imagine you are using the Symfony Templating component in which case this will not work. Or, you may also have your template file named improperly - it should be named after the controller and action name.
Better still, have a look at Symfony's Best Practices for Templates which recommends that you store templates in the templates/ directory of your root project, rather than a bundle's Resources/views/ folder. This means that you no longer reference templates like #App/Index/Index.html or use the magic #Template annotation. You would instead explicitly call your template from your controller like so:
/**
* #Route("", name="index")
*/
public function index()
{
return $this->render('index/index.html.twig');
}
Finally, and this may seem obvious, but make sure you have Twig installed in your project (composer require symfony/twig-bundle).

Doctrine PHPCR-ODM under Symfony not detecting mapped Document class

I am attempting to integrate PHPCR-ODM with an existing Symfony project, and am having trouble getting it to (presumably) detect my mapped Document class. Specifically, I get an error like this when attempting to persist a Document of my class MyDocument:
[Doctrine\Common\Persistence\Mapping\MappingException]
The class 'Example\Common\ORM\Document\MyDocument' was not found in the chain configured namespaces Doctrine\ODM\PHPCR\Document
My class is in a potentially strange namespace because this project uses Doctrine ORM as well, and thus far I've just added a new space for mapped Documents off of that, but I can't imagine the choice of namespace name affects the functionality.
Per the docs, I have added to my app/autoload.php:
AnnotationRegistry::registerFile(__DIR__.'/../vendor/doctrine/phpcr-odm/lib/Doctrine/ODM/PHPCR/Mapping/Annotations/DoctrineAnnotations.php');
My app/config/config.yml includes the following (with parameters set in parameters.yml):
doctrine_phpcr:
session:
backend:
type: jackrabbit
url: %jackrabbit_url%
workspace: %jackrabbit_workspace%
username: %jackrabbit_user%
password: %jackrabbit_password%
odm:
auto_mapping: true
My document class lives in src/Example/Common/ORM/Document/MyDocument.php and looks like:
<?php
namespace Example\Common\ORM\Document;
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM;
/**
* #PHPCRODM\Document
*/
class MyDocument
{
/**
* #PHPCRODM\Id
*/
private $id;
/**
* #PHPCRODM\ParentDocument
*/
private $parent;
/**
* #PHPCRODM\Nodename
*/
private $name;
// .. etc
Finally, the code I am using to test the integration is inside a simple console command, and looks like:
use Example\Common\ORM\Document\MyDocument;
// ...
$documentManager = $this->container->get('doctrine_phpcr.odm.default_document_manager');
$document = new MyDocument();
$document->setParent($documentManager->find(null, '/'));
$document->setName('ExampleName');
$documentManager->persist($document);
$documentManager->flush();
I have verified that my MyDocument class is being correctly loaded, but it seems that the annotations are not being processed in a way that is making the DocumentManager aware that it is a mapped Document class.
My guess is that I have overlooked some simple configuration step, but from looking repeatedly and thoroughly at the docs for PHPCR, PHPCR-ODM, and even Symfony CMF, I can't seem to find anything. Most of the examples out there involve using PHPCR via Symfony CMF, and I wasn't able to find many (any?) real world examples of PHPCR-ODM being integrated in a regular Symfony project.
edit: The Eventual Solution
I followed the advice that #WouterJ gave below and it fixed my problem, and I further followed his suggestion of adding a compiler pass to my Symfony bundle to make this work with a non-standard namespace (i.e., something other than YourBundle\Document). In my case, this is going into a library that will be re-used elsewhere rather than a bundle, so it was appropriate.
To do this, I added a method to the src/Example/Bundle/ExampleBundle/ExampleBundle.php file like so:
<?php
namespace Example\Bundle\ExampleBundle;
use Doctrine\Bundle\PHPCRBundle\DependencyInjection\Compiler\DoctrinePhpcrMappingsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class ExampleBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);
$mappedDirectories = array(
realpath(__DIR__ . '/../../Common/ODM/Document')
);
$mappedNamespaces = array(
'Example\Common\ODM\Document'
);
$phpcrCompilerClass = 'Doctrine\Bundle\PHPCRBundle\DependencyInjection\Compiler\DoctrinePhpcrMappingsPass';
if (class_exists($phpcrCompilerClass)) {
$container->addCompilerPass(
DoctrinePhpcrMappingsPass::createAnnotationMappingDriver(
$mappedNamespaces,
$mappedDirectories
));
}
}
}
That code allows any mapped document classes to be placed in the Example\Common\ODM\Document namespace and it will pick them up. This example uses annotations but the same pattern can be used for XML or YAML mappings (see the Doctrine\Bundle\PHPCRBundle\DependencyInjection\Compiler\DoctrinePhpcrMappingsPass class for method signatures).
I found that I also needed to define the doctrine_phpcr.odm.metadata.annotation_reader service for this to work, which I did in app/config.yml:
services:
doctrine_phpcr.odm.metadata.annotation_reader:
class: Doctrine\Common\Annotations\AnnotationReader
There may be a better way to do that, but that was enough to make it work for me.
The document should be placed in the Document namespace of the bundle, not the ORM\Document namespace.
If you really want to put it in the ORM\Document namespace (which is very strange, because we are talking about an ODM not an ORM), you can use the doctrine mapping compiler pass: http://symfony.com/doc/current/cookbook/doctrine/mapping_model_classes.html

Resources