我有这段代码:
<?php
$imageVar = $entry->field('logo')->generate();
$path = $imageVar;
$file = basename($path, ".svg");
$rest = substr($file, 0, -9);
?>
<img src="<?php echo $imageVar; ?>" onerror="this.onerror=null; this.src='files/media/images/partners/<?php echo $rest; ?>.png'" title="<?php echo $entry->field('name')->value(); ?>" alt="<?php echo $entry->field('name')->value(); ?>" >
它的工作但我收到这样的错误:
[13-Apr-2016 10:00:49 GMT]
PHP Warning: basename() expects parameter 1 to be string, object given in /wwwroot.wwwnew/templates/customcatalog_partnerpool_leser_1_1.html5 on line 58
#0 [internal function]: __error(2, 'basename() expe...', '/wwwroot.wwwnew...', 58, Array)
#1 /wwwroot.wwwnew/templates/customcatalog_partnerpool_leser_1_1.html5(58): basename(Object(Contao\FilesModel), '.svg')
#2 /wwwroot.wwwnew/system/modules/core/library/Contao/BaseTemplate.php(88): include('/wwwroot.wwwnew...')
#3 /wwwroot.wwwnew/system/modules/core/library/Contao/Template.php(277): Contao\BaseTemplate->parse()
#4 /wwwroot.wwwnew/system/modules/core/classes/FrontendTemplate.php(46): Contao\Template->parse()
#5 /wwwroot.wwwnew/system/modules/pct_customelements_plugin_customcatalog/PCT/CustomElements/Plugins/CustomCatalog/Core/CustomCatalog.php(436): Contao\FrontendTemplate->parse()
#6 /wwwroot.wwwnew/system/modules/pct_customelements_plugin_customcatalog/PCT/CustomElements/Plugins/CustomCatalog/Frontend/ModuleReader.php(145): PCT\CustomElements\Plugins\CustomCatalog\Core\CustomCatalog->render()
#7 /wwwroot.wwwnew/system/modules/core/modules/Module.php(283): PCT\CustomElements\Plugins\CustomCatalog\Frontend\ModuleReader->compile()
#8 /wwwroot.wwwnew/system/modules/pct_customelements_plugin_customcatalog/PCT/CustomElements/Plugins/CustomCatalog/Frontend/ModuleReader.php(95): Contao\Module->generate()
#9 /wwwroot.wwwnew/system/modules/core/elements/ContentModule.php(59): PCT\CustomElements\Plugins\CustomCatalog\Frontend\ModuleReader->generate()
#10 /wwwroot.wwwnew/system/modules/core/library/Contao/Controller.php(484): Contao\ContentModule->generate()
#11 /wwwroot.wwwnew/system/modules/core/modules/ModuleArticle.php(213): Contao\Controller::getContentElement(Object(Contao\ContentModel), 'main')
#12 /wwwroot.wwwnew/system/modules/core/modules/Module.php(283): Contao\ModuleArticle->compile()
#13 /wwwroot.wwwnew/system/modules/core/modules/ModuleArticle.php(67): Contao\Module->generate()
#14 /wwwroot.wwwnew/system/modules/core/library/Contao/Controller.php(417): Contao\ModuleArticle->generate(false)
#15 /wwwroot.wwwnew/system/modules/core/library/Contao/Controller.php(277): Contao\Controller::getArticle(Object(Contao\ArticleModel), true, false, 'main')
#16 /wwwroot.wwwnew/system/modules/core/pages/PageRegular.php(133): Contao\Controller::getFrontendModule('0', 'main')
#17 /wwwroot.wwwnew/system/modules/core/controllers/FrontendIndex.php(285): Contao\PageRegular->generate(Object(Contao\PageModel), true)
#18 /wwwroot.wwwnew/index.php(20): Contao\FrontendIndex->run()
#19 {main}
但我不知道如何解决这个问题。
答案 0 :(得分:3)
首先,你应该理解对象和字符串之间的差异
$imageVar = $entry->field('logo')->generate();
$path = $imageVar;
这里$ imageVar变成了对象而不是字符串所以你改变了第一个bez 你的函数$ file = basename($ path,&#34; .svg&#34;);需要字符串而不是对象
如果你在php中这样做,那么使用(字符串)befour $ imageVar现在你的代码将是
$imageVar = $entry->field('logo')->generate();
$path = (string)$imageVar;
试