问题
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
回答1:
Try
$icon = new Image('%kernel.root_dir%/appassBundle/Resources/Images/icon.png', 'icon');
Or maybe:
$iconPath = $this->get('kernel')->getRootDir().'/appassBundle/Resources/Images/icon.png';
$icon = new Image($iconPath, 'icon');
Or similar, I don't see whole structure of your project.
回答2:
Problem
The error tells it all. It means your Image
object tries to instanciate an SplFileObject 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
// apb/appassBundle/Controller/BarController::fooAction
$iconPath = sprintf("%s/../Resources/Images/icon.png", __DIR__);
$icon = new Image($iconPath, "icon");
$iconPath
now contains the absolute path to your icon. E.g:
/path/to/project/src/apb/appassBundle/Controller/../Resources/Images/icon.png
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 a getPath()
method which returns the absolute path of the bundle. (Root directory of the bundle)
// apb/appassBundle/Controller/BarController::fooAction
$bundle = $this->get('kernel')->getBundle('apbappassBundle');
$iconPath = sprintf("%s/Resources/Images/icon.png", $bundle->getPath());
$icon = new Image($iconPath, "icon");
$iconPath
now contains something like
/path/to/project/src/apb/appassBundle/Resources/Images/icon.png
Which is also correct
Edit 17/07/2014
As mentionned by gilden, an alternative would be to use the method KernelInterface::locateResource
You can use it like this
$iconPath = $this->get('kernel')->locateResource('@apbappassBundle/Resources/Images/icon.png');
$icon = new Image($iconPath, "icon");
来源:https://stackoverflow.com/questions/21594771/splfileobject-error-failed-to-open-stream-no-such-file-or-directory