i am trying to implement passbook web service in symfony2 and following this passbook bundle and my controller looks like this
if ($form->isValid()) {
// Create an event ticket
$pass = new EventTicket("1234567890", "The Beat Goes On");
$pass->setBackgroundColor('rgb(60, 65, 76)');
$pass->setLogoText('Apple Inc.');
// Create pass structure
$structure = new Structure();
// Add primary field
$primary = new Field('event', 'The Beat Goes On');
$primary->setLabel('Event');
$structure->addPrimaryField($primary);
// Add secondary field
$secondary = new Field('location', 'Moscone West');
$secondary->setLabel('Location');
$structure->addSecondaryField($secondary);
// Add auxiliary field
$auxiliary = new Field('datetime', '2013-04-15 @10:25');
$auxiliary->setLabel('Date & Time');
$structure->addAuxiliaryField($auxiliary);
// Add icon image
$icon = new Image('appassBundle/Resources/Images/icon.png', 'icon');
$pass->addImage($icon);
// Set pass structure
$pass->setStructure($structure);
// Add barcode
$barcode = new Barcode(Barcode::TYPE_QR, 'barcodeMessage');
$pass->setBarcode($barcode);
// Create pass factory instance
$factory = new PassFactory('pass.dk.mcoupons.mcoupon', '9W6X83AQ63', 'KA Innovation ApS', '%kernel.root_dir%/Resources/certificates/certificate.p12', 'hestmink09', '%kernel.root_dir%/Resources/certificates/applewwdrca.pem');
$factory->setOutputPath('%kernel.root_dir%/logs/pkpass');
$factory->package($pass);
//$em = $this->getDoctrine()->getEntityManager();
//$em->persist($task);
//$em->flush();
echo 'pass generated ';
return $this->render('apbappassBundle:Default:index.html.twig');
}
but it is giving me this error
SplFileObject::__construct(appassBundle/Resources/Images/icon.png): failed to open stream: No such file or directory 500 Internal Server Error - RuntimeException
i have tried different ways to give the path but failed. here is the hierarchy or folder structure where my images are stored
Problem
The error tells it all. It means your
Image
object tries to instanciate anSplFileObject
under the hood.Since the file you given does not exists,
SplFileObject::__construct()
throws an exception which is the one you're getting.Fix
Relative paths can easily be a nightmare, CLI SAPI et al. can mess everything. The simple fix is to use an ABSOLUTE path. I see two ways to handle it.
Absolute path relative to the current file
In order to get the current absolute path, you can use the
__DIR__
magic constant. Here's an example of usage$iconPath
now contains the absolute path to your icon. E.g:Which is correct, and the file actually exists.
Absolute path relative to the bundle
Another way is to get the path stored in the bundle.
BundleInterface
has agetPath()
method which returns the absolute path of the bundle. (Root directory of the bundle)$iconPath
now contains something likeWhich is also correct
As mentionned by gilden, an alternative would be to use the method KernelInterface::locateResource
You can use it like this
Try
Or maybe:
Or similar, I don't see whole structure of your project.