i'm following a tutorial online on how to use Symfony
i followed all the step but for some reason when is try to use NormalizeInterface i get this error :
Cannot determine controller argument for "App\Controller\ApiPostController::index()": the $normalizer argument is type-hinted with the non-existent class or interface: "App\Controller\NormalizerInterface". Did you forget to add a use statement?
I tried multiple solutions and none of them worked
My code is
<?php
namespace App\Controller;
use App\Repository\PostRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Normalizer;
class ApiPostController extends AbstractController
{
/**
* #Route("/api/post", name="api_post_index", methods={"GET"})
*/
public function index(PostRepository $postRepository, NormalizerInterface $normalizer )
{
$posts = $postRepository->findAll();
$postsNormalises = $normalizer->normalize($posts, null, ['groups' => 'post:read']);
return $this->render('api_post/index.html.twig', [
'controller_name' => 'ApiPostController',
]);
}
}
Thank you for taking the time to read and thank you for you help in advance
Add use line:
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
Related
Just playing around with laravel-8 unit tests. I extended the basic TestCase and thought laravels factory method would be available. I checked the composer.json and the factories are being loaded.
I am trying to run this particular test but factory is not found any ideas:
<?php
namespace Tests\Feature\Http\Controllers\Auth;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use App\User;
class LoginControllerTest extends TestCase
{
use RefreshDatabase;
/** #test */
public function login_authenticates_and_redirects_user()
{
$user = factory(User::class)->create();
$response = $this->post(route('login'), [
'email' => $user->email,
'password' => 'password'
]);
$response->assertRedirect(route('home'));
$this->assertAuthenticatedAs($user);
}
}
The error I am getting is:
1) Tests\Feature\Http\Controllers\Auth\LoginControllerTest::login_authenticates_and_redirects_user
Error: Call to undefined function Tests\Feature\Http\Controllers\Auth\factory()
On laravel 8 models are at 'App\Models\'.
It changes how factory works. See at docs.
So, it should be like:
<?php
namespace Tests\Feature\Http\Controllers\Auth;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use App\Models\User;
class LoginControllerTest extends TestCase
{
use RefreshDatabase;
/** #test */
public function login_authenticates_and_redirects_user()
{
$user = User::factory->create();
$response = $this->post(route('login'), [
'email' => $user->email,
'password' => 'password'
]);
$response->assertRedirect(route('home'));
$this->assertAuthenticatedAs($user);
}
}
Turns out in upgrading to laravel-8 release notes:
https://laravel.com/docs/8.x/upgrade#seeder-factory-namespaces
"Laravel's model factories feature has been totally rewritten to support classes and is not compatible with Laravel 7.x style factories."
So in order to make it work I used:
$user = \App\Models\User::factory(User::class)->make();
Good morning guys,
I have a probleme on my symfony 4 Api.
I should return a json response, but the serializer return a string with slashes. I Don't know how escape it.
Bellow my controller :
use App\Entity\Category;
use App\Form\CategoryType;
use App\Repository\CategoryRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\SerializerInterface as SerializerInterfaceAlias;
use FOS\RestBundle\Controller\Annotations as Rest;
/**
* Category Controller
* #Route("/api", name="api_")
*/
class CategoryController extends AbstractController
{
/**
* #Rest\Get("/categories")
* #param CategoryRepository $categoryRepository
* #param SerializerInterfaceAlias $serializer
*/
public function index(CategoryRepository $categoryRepository, SerializerInterfaceAlias $serializer)
{
$jsonContent = $serializer->serialize($categoryRepository->findall(), 'json', ['json_encode_options' => JSON_UNESCAPED_SLASHES]);
return $jsonContent;
}
[....]
}
And my return is look like :
"[{\"id\":3,\"name\":\"toto\",\"logo\":\"tata\",\"description\":\"lolo\",\"dateCreated\":\"2019-05-09T10:30:39+00:00\"},{\"id\":4,\"name\":\"tata\",\"logo\":\"titi\",\"description\":\"tutu\",\"dateCreated\":\"2019-05-09T10:30:49+00:00\"}]"
For information I using PHP 7.1 & Symfony 4.2.
So I want a proper json format... without this slashes :(
Do you have any suggestion ? :)
Thanks in advance !
I finaly resolve my problem #RubberDuckDebugging
I don't need to use the serializer here.
I need just to return :
return $this->json($categoryRepository->findall());
That's so simple. Sorry :)
Since the last 4 hours I'm trying to understand the logic of Symfony 2 services and how they integrate in the application...
Basically I'm trying to set my EntityManager via a service and use it in a controller
I have the following structure
Bundle1/Controller/Bundle1Controller.php
Bundle1/Services/EntityService.php
Bundle2/Controller/Bundle2Controller.php
Bundle3/Controller/Bundle3Controller.php
....
I'm trying to make a REST API with different entry points, that's why I use multiple bundles bundle2,bundle3....
The logic is the following:
A POST is fired to Bundle2/Controller/Bundle2Controller.php
Bundle2Controller.php instances a new() Bundle1Controller.php
Inside Bundle1Controller I want to access a service entity_service in order to get my EntityManager
I have 2 cases in which I manage to land...
In Bundle1/Controller/Bundle1Controller if I try $this->container or $this->get('entity_service') I get a null everytime
If I set the container in Bundle2/Controller/Bundle2Controller and try $this->get('entity_service') I get You have requested a non-existent service "entity_service"
I will place all the code below
Bundle1/Controller/Bundle1Controller
<?php
namespace Bundle1\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use EntityBundle\Entity\TestEntity;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
class Bundle1Controller extends Controller
{
/**
* #param $response
* #return array
*/
public function verifyWebHookRespone($response){
$em = $this->get('entity_service')->getEm();
$array = json_decode($response);
$mapping = $em->getRepository('EntityBundle:TestEntity')
->findBy(["phone" => $array['usernumber']]);
return $mapping;
}
}
Bundle2/Controller/Bundle2Controller.php
<?php
namespace Bundle2\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Bundle1\Controller\Bundle1Controller;
class Bundle2Controller extends Controller
{
public function webhookAction(Request $request)
{
$data = $request->request->get('messages');
$model = new Bundle1Controller();
$responseMessage = $model->verifyWebHookRespone($data);
return new Response($responseMessage, Response::HTTP_CREATED, ['X-My-Header' => 'My Value']);
}
}
Bundle1/Services/EntityService.php
<?php
namespace EntityBundle\Services;
use Doctrine\ORM\EntityManager;
use Symfony\Component\DependencyInjection\Container;
class EntityService
{
protected $em;
private $container;
public function __construct(EntityManager $entityManager, Container $container)
{
$this->em = $entityManager;
$this->container = $container;
}
/**
* #return EntityManager
*/
public function getEm()
{
return $this->em;
}
}
services.yml
services:
entity_service:
class: Bundle1\Services\EntityService
arguments: [ "#doctrine.orm.entity_manager" , "#service_container" ]
Can anyone please help me with something regarding this issue?
How can I register a service and call it from anywhere no matter the bundle or another service?
You should check where your services.yml is located and whether it is imported in the config.yml
You can't just instantiate a controller and expect it to work, you need to set the container.
But you can call EntityManager without needing any other service by using;
$this->get('doctrine.orm.entity_manager');
I can't understand your structure or what you are trying to achieve, but those are the options to go about if you want to keep this structure.
I'm working on a project, but I have a very annoying problem. I use a PHP file rb.php that contains several important classes for the project (File rb.php of the RedBean ORM, all in one).
The problem is that I can use the file correctly with a require in a special location, but not in another location.
This is my arborescence:
When I go to index.php, everything goes well, i can do require('rb.php');
<?php
require_once 'vendor/autoload.php';
require('rb.php');
R::setup('mysql:host=localhost;
dbname=silex','root','');
require('Model_Bandmember.php');
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
$srcDir = __DIR__;
$app = new Application();
$app['debug'] = true;
$app->register(new DDesrosiers\SilexAnnotations\AnnotationServiceProvider(), array(
"annot.controllerDir" => $srcDir."\controllers"
));
$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => $srcDir.'\views',
));
$bandmember = R::dispense('bandmember');
$bandmember->name = 'Fatz Waller';
$id = R::store($bandmember);
$bandmember = R::load('bandmember',$id);
R::trash($bandmember);
echo $lifeCycle;die();
$app->run();
I have the good value of $lifeCycle. But I would like to use this file in a controller for functions add (), updates () etc .. So I try this :
<?php
namespace App\Controllers;
use DDesrosiers\SilexAnnotations\Annotations as SLX;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
require(__DIR__.'/../rb.php');
/**
* #SLX\Controller(prefix="article")
*/
class ArticleController
{
/**
* #SLX\Route(
* #SLX\Request(method="GET", uri="/"),
* #SLX\Bind(routeName="articleIndex")
* )
*/
public function index(Application $app)
{
$articles = R::findAll('article');
return $app['twig']->render('Article/index.twig', array(
'articles' => $articles,
));
}
...
...
But i have this error :
Cannot redeclare class RedBeanPHP\RedException in C:\wamp64\www\SilexTest\rb.php on line 6737
Very well, I think that the file must already be present! But if i comment it i have this error :
Class 'App\Controllers\R' not found
This is normal because this class is in the rb.php file that I just commented on.
If I do a require, I have a class redeclare , but if I do not put it, it lacks a class.
Any help will be appreciated.
Since the rb is already included so no need to include it anywhere. To use it from the global scope , you've to use \R:
$articles = \R::findAll('article');
Because, it seems like that, the R is available in the global scope. In this case, you can use use R; at the top of your class, for example:
namespace App\Controllers;
use DDesrosiers\SilexAnnotations\Annotations as SLX;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use R; // <-- Notice this
/**
* #SLX\Controller(prefix="article")
*/
class ArticleController
{
// Use: R::findAll('article') in any method in this class
}
You should read about namespace in PHP.
Warning: This question is Laravel 4 specific.
I've been using Facades in my controllers before. Therefore I know the code is working. Now I need to introduce dependency injection for various reasons.
After refactoring the controller I get following error:
Illuminate \ Container \ BindingResolutionException
Unresolvable dependency resolving [Parameter #0 [ $name ]].
I can't figure out where the problem is. The Error message seems cryptic to me and I don't understand it. (I don't see any problem with my __constructor parameters since I've registered the binding for the HelpersInterface)
Here are the important parts of my code:
File: app/start/global.php
<?php
// ...
App::bind('Acme\Interfaces\HelpersInterface', 'Acme\Services\Helpers');
File: composer.json
// ...
"autoload": {
// ...
"psr-0": {
"Acme": "app/"
}
},
// ...
File: app/Acme/Controllers/BaseController.php
<?php namespace Acme\Controllers;
use Carbon\Carbon;
use Controller;
use Illuminate\Foundation\Application as App;
use Illuminate\View\Factory as View;
use Acme\Interfaces\HelpersInterface as Helpers;
use Illuminate\Http\Response;
class BaseController extends Controller {
/**
* #var \Illuminate\Foundation\Application
*/
private $app;
/**
* #var \Carbon\Carbon
*/
private $carbon;
/**
* #var \Illuminate\View\Factory
*/
private $view;
/**
* #var \Acme\Interfaces\HelpersInterface
*/
private $helpers;
function __construct(App $app, Carbon $carbon, View $view, Helpers $helpers)
{
$this->app = $app;
$this->carbon = $carbon;
$this->view = $view;
$this->helpers = $helpers;
$lang = $this->app->getLocale();
$now = $this->carbon->now();
$this->view->share('lang', $lang);
$this->view->share('now', $now);
}
/**
* Missing Method
*
* Abort the app and return a 404 response
*
* #param array $parameters
* #return Response
*/
public function missingMethod($parameters = array())
{
return $this->helpers->force404();
}
}
File: app/Acme/Services/Helpers.php
<?php namespace Acme\Services;
use Illuminate\Config\Repository as Config;
use Illuminate\Database\Connection as DB;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector as Redirect;
use Illuminate\Session\Store as Session;
use Illuminate\Support\Facades\Response;
use Illuminate\Translation\Translator as Lang;
use Illuminate\View\Factory as View;
use Acme\Interfaces\MockablyInterface;
use Monolog\Logger as Log;
class Helpers implements HelpersInterface {
// ...
public function __construct(
Config $config,
Lang $lang,
View $view,
MockablyInterface $mockably,
Log $log,
Request $request,
Session $session,
DB $db,
Redirect $redirect,
Response $response
) {
// ...
}
// ...
}
File: app/Acme/Providers/HelpersServiceProvider.php
<?php namespace Acme\Providers;
use Illuminate\Support\ServiceProvider;
use Acme\Services\Helpers;
class HelpersServiceProvider extends ServiceProvider {
private $db;
private $defaultDbConnection;
protected function init()
{
$this->db = $this->app['db'];
$this->defaultDbConnection = $this->db->getDefaultConnection();
}
public function register()
{
$this->init();
$this->app->bind('helpers', function ()
{
return new Helpers(
$this->app['config'],
$this->app['translator'],
$this->app['view'],
$this->app['mockably'],
$this->app->make('log')->getMonolog(),
$this->app['request'],
$this->app['session.store'],
$this->db->connection($this->defaultDbConnection),
$this->app['redirect'],
$this->app['Illuminate\Support\Facades\Response']
);
});
}
For me it was just a matter of running
php artisan optimize:clear
It seems your Acme\Services\Helpers constructor takes a $name parameter, but is not type hinted.
Laravel's IoC is not magic. If your don't provide a type hint for every parameter, the IoC container has no way of knowing what to pass in.
Make sure you use Illuminate\Http\Request; on top of the file instead of any other http import like this
use Illuminate\Http\Request;
THANK ME LATER!
Got it fixed. All the tutorials about dependency injection were referring to concrete implementations of interfaces so that I thought that's the way to go about it. Joseph Silber's answer got me on the right track.
The trick is to bind the Interface to the binding of the ServiceProvider like shown below. That way Laravel will know how to instantiate the Helpers service.
File: app/start/global.php
<?php
// ...
App::bind('Acme\Interfaces\HelpersInterface', 'helpers');