ประเภทบล็อกไม่ถูกต้อง: [Namespace] \ [Module] \ Block \ Timeline


11

ฉันพยายามเพิ่มบล็อกไปยังส่วนท้าย แต่จนถึงตอนนี้มันกลับมาแล้ว Invalid block type: [Namespace]\[Module]\Block\Timeline

แหล่งที่มาของสิ่งที่ฉันมีจนถึงสามารถพบได้ที่นี่: https://github.com/sandermangel/magento2-twitterfeed

ของฉัน layout/default.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="footer">
            <block class="[Namespace]\[Module]\Block\Timeline" name="twitterfeed-timeline" template="footer/timeline.phtml"></block>
        </referenceBlock>
    </body>
</page>

และคลาสบล็อกของฉันใน vendor/[namespace]/[module]/Block/Timeline.php

namespace [Namespace]\[Module]\Block;

use Magento\Framework\View\Element\Template;

class Timeline extends Template
{

    /**
     * Timeline constructor.
     * @param Template\Context $context
     * @param array $data
     */
    public function __construct(
        Template\Context $context,
        array $data = []
    ) {
        parent::__construct($context, $data);
    }

    /**
     * @return mixed
     */
    public function getTweets()
    {
        // ---
    }
}

composer.json

{
  "name": "[namespace]/[module]",
  "description": "Lipsum",
  "version": "0.1.0",
  "license": "OSL-3.0",
  "require": {
    "abraham/twitteroauth": "*"
  },
  "type": "magento2-module",
  "repositories": [
    {
      "type": "git",
      "url": "https://github.com/url.git"
    },
    {
      "type": "git",
      "url": "https://github.com/abraham/twitteroauth.git"
    }
  ],
  "autoload": {
    "files": [
      "registration.php"
    ],
    "psr-4": {
      "[Namespace]\\[Module]\\": ""
    }
  }
}

และเกิดexception.logข้อผิดพลาด

[2015-11-29 16:56:55] main.CRITICAL: exception 'Magento\Framework\Exception\LocalizedException' with message 'Invalid block type: [Namespace]\[Module]\Block\Timeline' in vendor/magento/framework/View/Layout/Generator/Block.php:262
Stack trace:
#0 vendor/magento/framework/View/Layout/Generator/Block.php(237): Magento\Framework\View\Layout\Generator\Block->getBlockInstance('[Namespace]\\[Module]...', Array)
#1 vendor/magento/framework/View/Layout/Generator/Block.php(216): Magento\Framework\View\Layout\Generator\Block->createBlock('[Namespace]\\[Module]...', 'twitterfeed-tim...', Array)
#2 vendor/magento/framework/View/Layout/Generator/Block.php(124): Magento\Framework\View\Layout\Generator\Block->generateBlock(Object(Magento\Framework\View\Layout\ScheduledStructure), Object(Magento\Framework\View\Layout\Data\Structure), 'twitterfeed-tim...')
#3 vendor/magento/framework/View/Layout/GeneratorPool.php(86): Magento\Framework\View\Layout\Generator\Block->process(Object(Magento\Framework\View\Layout\Reader\Context), Object(Magento\Framework\View\Layout\Generator\Context))
#4 vendor/magento/framework/View/Layout.php(327): Magento\Framework\View\Layout\GeneratorPool->process(Object(Magento\Framework\View\Layout\Reader\Context), Object(Magento\Framework\View\Layout\Generator\Context))
#5 var/generation/Magento/Framework/View/Layout/Interceptor.php(89): Magento\Framework\View\Layout->generateElements()
#6 vendor/magento/framework/View/Layout/Builder.php(129): Magento\Framework\View\Layout\Interceptor->generateElements()
#7 vendor/magento/framework/View/Page/Builder.php(55): Magento\Framework\View\Layout\Builder->generateLayoutBlocks()
#8 vendor/magento/framework/View/Layout/Builder.php(65): Magento\Framework\View\Page\Builder->generateLayoutBlocks()
#9 vendor/magento/framework/View/Page/Config.php(166): Magento\Framework\View\Layout\Builder->build()
#10 vendor/magento/framework/View/Page/Config.php(475): Magento\Framework\View\Page\Config->build()
#11 vendor/magento/framework/View/Page/Config.php(433): Magento\Framework\View\Page\Config->getElementAttribute('body', 'class')
#12 vendor/magento/module-catalog/Helper/Product/View.php(159): Magento\Framework\View\Page\Config->addBodyClass('catalog-product...')
#13 vendor/magento/module-catalog/Helper/Product/View.php(214): Magento\Catalog\Helper\Product\View->initProductLayout(Object(Magento\Framework\View\Result\Page\Interceptor), Object(Magento\Catalog\Model\Product\Interceptor), Object(Magento\Framework\DataObject))
#14 vendor/magento/module-catalog/Controller/Product/View.php(109): Magento\Catalog\Helper\Product\View->prepareAndRender(Object(Magento\Framework\View\Result\Page\Interceptor), 1, Object(Magento\Catalog\Controller\Product\View\Interceptor), Object(Magento\Framework\DataObject))
#15 var/generation/Magento/Catalog/Controller/Product/View/Interceptor.php(24): Magento\Catalog\Controller\Product\View->execute()
#16 vendor/magento/framework/App/Action/Action.php(102): Magento\Catalog\Controller\Product\View\Interceptor->execute()
#17 [internal function]: Magento\Framework\App\Action\Action->dispatch(Object(Magento\Framework\App\Request\Http))
#18 vendor/magento/framework/Interception/Interceptor.php(74): call_user_func_array(Array, Array)
#19 vendor/magento/framework/Interception/Chain/Chain.php(70): Magento\Catalog\Controller\Product\View\Interceptor->___callParent('dispatch', Array)
#20 vendor/magento/framework/Interception/Chain/Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Catalog...', 'dispatch', Object(Magento\Catalog\Controller\Product\View\Interceptor), Array, 'designLoader')
#21 vendor/magento/framework/App/Action/Plugin/Design.php(39): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#22 [internal function]: Magento\Framework\App\Action\Plugin\Design->aroundDispatch(Object(Magento\Catalog\Controller\Product\View\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#23 vendor/magento/framework/Interception/Chain/Chain.php(68): call_user_func_array(Array, Array)
#24 vendor/magento/framework/Interception/Chain/Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Catalog...', 'dispatch', Object(Magento\Catalog\Controller\Product\View\Interceptor), Array, 'customer-app-ac...')
#25 vendor/magento/module-customer/Model/App/Action/ContextPlugin.php(61): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#26 [internal function]: Magento\Customer\Model\App\Action\ContextPlugin->aroundDispatch(Object(Magento\Catalog\Controller\Product\View\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#27 vendor/magento/framework/Interception/Chain/Chain.php(68): call_user_func_array(Array, Array)
#28 vendor/magento/framework/Interception/Chain/Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Catalog...', 'dispatch', Object(Magento\Catalog\Controller\Product\View\Interceptor), Array, 'storeCheck')
#29 vendor/magento/module-store/App/Action/Plugin/StoreCheck.php(44): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#30 [internal function]: Magento\Store\App\Action\Plugin\StoreCheck->aroundDispatch(Object(Magento\Catalog\Controller\Product\View\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#31 vendor/magento/framework/Interception/Chain/Chain.php(68): call_user_func_array(Array, Array)
#32 vendor/magento/framework/Interception/Chain/Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Catalog...', 'dispatch', Object(Magento\Catalog\Controller\Product\View\Interceptor), Array, 'contextPlugin')
#33 vendor/magento/module-store/App/Action/Plugin/Context.php(98): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#34 [internal function]: Magento\Store\App\Action\Plugin\Context->aroundDispatch(Object(Magento\Catalog\Controller\Product\View\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#35 vendor/magento/framework/Interception/Chain/Chain.php(68): call_user_func_array(Array, Array)
#36 vendor/magento/framework/Interception/Interceptor.php(136): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Catalog...', 'dispatch', Object(Magento\Catalog\Controller\Product\View\Interceptor), Array, 'tax-app-action-...')
#37 vendor/magento/module-tax/Model/App/Action/ContextPlugin.php(91): Magento\Catalog\Controller\Product\View\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#38 [internal function]: Magento\Tax\Model\App\Action\ContextPlugin->aroundDispatch(Object(Magento\Catalog\Controller\Product\View\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#39 vendor/magento/framework/Interception/Interceptor.php(141): call_user_func_array(Array, Array)
#40 var/generation/Magento/Catalog/Controller/Product/View/Interceptor.php(39): Magento\Catalog\Controller\Product\View\Interceptor->___callPlugins('dispatch', Array, Array)
#41 vendor/magento/framework/App/FrontController.php(55): Magento\Catalog\Controller\Product\View\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#42 [internal function]: Magento\Framework\App\FrontController->dispatch(Object(Magento\Framework\App\Request\Http))
#43 vendor/magento/framework/Interception/Interceptor.php(74): call_user_func_array(Array, Array)
#44 vendor/magento/framework/Interception/Chain/Chain.php(70): Magento\Framework\App\FrontController\Interceptor->___callParent('dispatch', Array)
#45 vendor/magento/framework/Interception/Chain/Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Framewo...', 'dispatch', Object(Magento\Framework\App\FrontController\Interceptor), Array, 'requestPreproce...')
#46 vendor/magento/module-store/App/FrontController/Plugin/RequestPreprocessor.php(89): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#47 [internal function]: Magento\Store\App\FrontController\Plugin\RequestPreprocessor->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#48 vendor/magento/framework/Interception/Chain/Chain.php(68): call_user_func_array(Array, Array)
#49 vendor/magento/framework/Interception/Chain/Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Framewo...', 'dispatch', Object(Magento\Framework\App\FrontController\Interceptor), Array, 'install')
#50 vendor/magento/framework/Module/Plugin/DbStatusValidator.php(69): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#51 [internal function]: Magento\Framework\Module\Plugin\DbStatusValidator->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#52 vendor/magento/framework/Interception/Chain/Chain.php(68): call_user_func_array(Array, Array)
#53 vendor/magento/framework/Interception/Chain/Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Framewo...', 'dispatch', Object(Magento\Framework\App\FrontController\Interceptor), Array, 'storeCookieVali...')
#54 vendor/magento/module-store/Model/Plugin/StoreCookie.php(78): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#55 [internal function]: Magento\Store\Model\Plugin\StoreCookie->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#56 vendor/magento/framework/Interception/Chain/Chain.php(68): call_user_func_array(Array, Array)
#57 vendor/magento/framework/Interception/Chain/Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Framewo...', 'dispatch', Object(Magento\Framework\App\FrontController\Interceptor), Array, 'front-controlle...')
#58 vendor/magento/module-page-cache/Model/App/FrontController/VarnishPlugin.php(55): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#59 [internal function]: Magento\PageCache\Model\App\FrontController\VarnishPlugin->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#60 vendor/magento/framework/Interception/Chain/Chain.php(68): call_user_func_array(Array, Array)
#61 vendor/magento/framework/Interception/Interceptor.php(136): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Framewo...', 'dispatch', Object(Magento\Framework\App\FrontController\Interceptor), Array, 'front-controlle...')
#62 vendor/magento/module-page-cache/Model/App/FrontController/BuiltinPlugin.php(68): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#63 [internal function]: Magento\PageCache\Model\App\FrontController\BuiltinPlugin->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#64 vendor/magento/framework/Interception/Interceptor.php(141): call_user_func_array(Array, Array)
#65 var/generation/Magento/Framework/App/FrontController/Interceptor.php(26): Magento\Framework\App\FrontController\Interceptor->___callPlugins('dispatch', Array, Array)
#66 vendor/magento/framework/App/Http.php(115): Magento\Framework\App\FrontController\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#67 vendor/magento/framework/App/Bootstrap.php(258): Magento\Framework\App\Http->launch()
#68 index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http))
#69 {main} [] []

ความคิดเกี่ยวกับสิ่งที่อาจจะผิดใด ๆ ชื่นชม


'ผู้จำหน่าย / [namespace] / [โมดูล] /Block/Timeline.php' พา ธ นี้ควรเป็น 'app / code / [Namespace] / [โมดูล] /Block/Timeline.php'
Sohel Rana

คุณมีข้อผิดพลาดในบันทึกหรือไม่
KAndy

@KAndy ฉันได้อัพเดทพร้อมบันทึกข้อยกเว้นแล้ว มันเป็นข้อผิดพลาด + backtrace
Sander Mangel

หมายความว่าไม่สามารถโหลดคลาสของคุณได้ คุณได้ลงทะเบียนโมดูลในผู้แต่งหรือไม่? คุณช่วยเพิ่มผู้แต่งได้ไหม
KAndy

@KAndy ขออภัยสำหรับการตอบกลับล่าช้า เพิ่มผู้แต่ง.json คุณถูก. ล้มเหลวในการรับอินสแตนซ์Magento\Framework\View\Layout\Generator\Block::getBlockInstanceแต่ฉันไม่สามารถระบุได้ว่าทำไม
Sander Mangel

คำตอบ:


12

ฉันกำหนด a useในคลาสบล็อกก่อนการกำหนดคลาส ในการใช้งานนี้ฉันลืมผู้นำ\ที่กรณีข้อผิดพลาด แต่เนื่องจากข้อผิดพลาดดังกล่าวถูกดักจับและระงับในMagento\Framework\View\Layout\Generator\Block::getBlockInstanceนั้นจึงทำให้เกิดข้อผิดพลาดที่ไม่เกี่ยวข้องทั้งหมด

ในระยะสั้น: ก่อให้เกิดข้อผิดพลาด PHP ข้างต้นclassคำนิยามจะส่งผลให้เกิดInvalid block type:ข้อผิดพลาด


ฉันมีปัญหาที่คล้ายกันในขณะนี้: ฉันลืมที่จะประกาศบางคำสั่ง "ใช้" สำหรับบางคลาส รายงาน "ประเภทบล็อกไม่ถูกต้อง" ไม่เกี่ยวข้องกับปัญหาจริง ฉันใช้เวลานานกว่าจะคิดออก
peedee

1

ฉันได้รับการแก้ไขเมื่อฉันเปลี่ยนชื่อไฟล์เป็นชื่อคลาสและการเรียก XML แบบนี้:

app / รหัส / Rafaelcg / Smartlook / view / ส่วนหน้า / รูปแบบ / default.xml

<referenceContainer name="after.body.start">
            <block class="Rafaelcg\Smartlook\Block\Code" name="smartlook" as="smartlook" template="smartlook.phtml"/>
 </referenceContainer>

app / รหัส / Rafaelcg / Smartlook / บล็อก / Code.php

<?php   

namespace Rafaelcg\Smartlook\Block;

use \Magento\Framework\View\Element\Template;

class Code extends Template
{

0

หากคุณใช้ Composer เพื่อรวมโมดูลของคุณนี่เป็นอีกสาเหตุที่เป็นไปได้สำหรับข้อผิดพลาดนั้น:

คุณอาจมีผิด PSR-4 composer.jsonนิยามของคุณ โดยปกติควรมีลักษณะเช่นนี้:

"autoload": {
    "psr-4": {
        "Namespace\\Module\\": ""
    }
}

อย่างไรก็ตามถ้าชื่อโมดูลของคุณไม่ได้การ Moduleโหลดอัตโนมัติของคุณจะไม่สามารถใช้งานได้และจะไม่พบคลาส ในกรณีนี้คุณต้องแก้ไขชื่อโมดูลและเรียกใช้composer require namespace/moduleอีกครั้งเพื่ออัปเดตคำจำกัดความในcomposer.lockและเพื่อสร้างไฟล์ autoload ของคุณอีกครั้ง

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.