为什么在Symfony2中找不到我的SerializerBuilder?
I'm trying to use the JMSSerializerBuilder
to encode my objects in json to be able to make AJAX calls.
I've succesfully installed the bundle through composer.
Then, following the official documentation, I'm doing:
<?php
namespace Pondip\GeolocBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use JMS\Serializer\SerializerBuilder;
class DefaultController extends Controller
{
public function getLakesSurroundingAction($lat=0, $lng=0, $limit = 50, $distance = 50, $unit = 'km')
{
$lakesNearby= $this->getNearby($lat, $lng, $limit, $distance, $unit);
$serializer = JMS\Serializer\SerializerBuilder::create()->build();
$return = $serializer->serialize($return, 'json');
}
}
But it returns
FatalErrorException:
Error: Class 'Pondip\GeolocBundle\Controller\JMS\Serializer\SerializerBuilder' not found in C:\Program Files (x86)\wamp\www\Pondip Dev\src\Pondip\GeolocBundle\Controller\DefaultController.php line 51
line 51 is:
$serializer = JMS\Serializer\SerializerBuilder::create()->build();
Why is that happening?
When removing the use JMS\Serializer\SerializerBuilder;
line (since it is not specified in the doc) I just get an internal server error.
我正在尝试使用 我已经通过作曲家成功安装了这个包。 p>
然后,在官方文档,我正在做: p>
但它返回 p>
FatalErrorException: p>
错误:在C:\ Program中找不到类
'Pondip \ GeolocBundle \ Controller \ JMS \ Serializer \ SerializerBuilder' 文件(x86)\ wamp \ www \ Pondip
Dev \ src \ Pondip \ GeolocBundle \ Controller \ DefaultController.php第51行 p>
blockquote>
第51行是: p>
为什么 发生了什么?
当删除 JMSSerializerBuilder code>对json中的对象进行编码以便能够 进行AJAX调用。 p>
&lt;?php
namespace Pondip \ GeolocBundle \ Controller;
use Symfony \ Bundle \ FrameworkBundle \ Controller \ Controller;
use Symfony \ Component \ HttpFoundation \ Response;
use JMS \ Serializer \ SerializerBuilder;
class DefaultController扩展Controller
{
公共函数getLakesSurroundingAction($ lat = 0,$ lng = 0,$ limit = 50,$ distance = 50,$ unit ='km')
{
$ lakesNearby = $ this-&gt; getNearby($ lat,$ lng,$ limit,$ distance,$ unit);
$ serializer = JMS \ Serializer \ SerializerBuilder :: create() - &gt; build();
$ return = $ serializer-&gt; serialize($ return,'json');
}
}
code> pre>
$ serializer = JMS \ Serializer \ SerializerBuilder :: create() - &gt; build();
code> pre>
时使用JMS \ Serializer \ SerializerBuilder; code>行(因为它没有在doc中指定)我得到一个 n内部服务器错误。 p>
div>
That's because you don't use namespaces correctly. An use statements generates an alias for a specific namespace. The use JMS\Serializer\SerializerBuilder;
statement means that SerializerBuilder
is an alias for the JMS\Serializer\SerializerBuilder
class.
When doing $serializer = JSM\Serializer\SerializerBuilder::create()
in your code, it is an unqualified-namespace, which means it gets into the current namespace. That means PHP is searching for a Pondip\GeolocBundle\Controller\JMS\Serializer\SerializerBuilder
(as you're in the Pondip\GeolocBundle\Controller\
namespace).
To fix this, just use $serializer = SerializerBuilder::create()
. Because we've set up an alias for the SerializerBuilder
, it gets to the correct class and everything works.
For more information, please take a look at some "PHP namespacing" tutorials, like this one.
With use JMS\Serializer\SerializerBuilder;
in place you should address the class just as SerializerBuilder
. Without it, use the fully qualified name \JMS\Serializer\SerializerBuilder
(notice the leading backslash!)
Further reference: http://www.php.net/manual/en/language.namespaces.basics.php
I personnaly had to use $this->container->get('serializer');
instead of $serializer = $container->get('jms_serializer');