Symfony错误:“在呈现模板期间引发了异常”

时间:2018-07-18 04:10:30

标签: php symfony twig rendering symfony4

我正在尝试

<link href="{{ asset('css/mystyle.css') }}"
      rel="stylesheet"/> 

在我的树枝文件中进行渲染。但这给了我未捕获的PHP异常Twig_Error_Runtime:

  

“在呈现​​模板期间引发了异常   (“资产清单文件“ /../public/build/manifest.json”不   存在。”

当我使用

时,它确实可以工作
link href="css/mystyle.css"
          rel="stylesheet"/>

。 以下是我的控制器:

    <?php

namespace App\Controller;

use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

class WelcomeController extends AbstractController
{
    /**
     * @Route("/", name="welcome")
     */
    public function index()
    {
        return $this->render('welcome/index.html.twig');
    }
}

以下是我的树枝模板:

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Let's Explore Symfony 4</title>

    <!-- Bootstrap core CSS -->
    <link rel="stylesheet"
          href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css"
          integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy"
          crossorigin="anonymous">

    <link href="{{ asset('css/mystyle.css') }}"
          rel="stylesheet"/>

</head>

<body>
<header>
    <nav class="navbar navbar-expand-sm navbar-dark bg-dark">
        <div class="container">

            <a class="navbar-brand" href="#">Home</a>

            <div class="collapse navbar-collapse">
                <ul class="navbar-nav mr-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="#">Hello Page</a>
                    </li>
                </ul>
            </div>
        </div>

    </nav>
</header>

<main role="main" class="container main">
    <div>
        <h1>Let's Explore Symfony 4</h1>
        <p class="lead">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras rutrum sapien mauris, venenatis
            facilisis neque tincidunt vel. Maecenas vel felis vel turpis scelerisque eleifend. Fusce nec purus egestas,
            efficitur nisi ac, ultrices nulla. Pellentesque eu mollis tortor, in mollis nisl. Maecenas rhoncus quam non
            lacinia mollis.</p>
    </div>
</main>
</body>
</html>

我该如何解决此问题?

7 个答案:

答案 0 :(得分:7)

执行以下命令:

yarn add --dev @symfony/webpack-encore

yarn add webpack-notifier --dev

yarn encore dev

它将生成manifest.json文件

答案 1 :(得分:1)

yarn encore dev-server --hot创建manifest.json文件

答案 2 :(得分:0)

尝试安装Assets Bundle

composer require symfony/asset

答案 3 :(得分:0)

manifest.json是Symfony 3.3中的新功能。

因此,您应该在配置文件中包含以下行:

// The url of the service 
$url = 'https://wsbexpress.dhl.com:443/sndpt/expressRateBook?WSDL';
// the soap operation which is called
$action = 'createShipmentRequest';

// the xml input of the service
$xmlrequest = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:rat="http://scxgxtt.phx-dc.dhl.com/euExpressRateBook/RateMsgRequest">

    <soapenv:Header>

     <wsse:Security soapenv:mustunderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
         <wsse:UsernameToken wsu:id="UsernameToken-5" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">   
            <wsse:Username>*********</wsse:Username>           
            <wsse:Password type="PasswordText">*******</wsse:Password>
            <wsse:Nonce encodingtype="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">eUYebYfsjztETJ4Urt8AJw==</wsse:Nonce>
            <wsu:Created>' . date('Y-m-d H:i:s') . '</wsu:Created>
         </wsse:UsernameToken>
      </wsse:Security>

   <soapenv:Header/>
   <soapenv:Body>
     <rat:RateRequest>
    <RequestedShipment>
    <DropOffType>REGULAR_PICKUP</DropOffType>
          <Account>407194546</Account>
            <Currency>EUR</Currency>
          <UnitOfMeasurement>SI</UnitOfMeasurement>
         <Ship>
            <Shipper>
                <StreetLines>Street number 22</StreetLines>
                <City>City</City>             
                <PostalCode>111111</PostalCode>
                <CountryCode>DE</CountryCode>
            </Shipper>
            <Recipient>
                <StreetLines>Street number 22</StreetLines>
                <City>City</City>             
                <PostalCode>111111</PostalCode>
                <CountryCode>DE</CountryCode>
            </Recipient>
        </Ship>
        <Packages>
            <RequestedPackages number="1">
                <Weight>
                    <Value>0.5</Value>
                </Weight>
                <Dimensions>
                    <Length>3</Length>
                    <Width>2</Width>
                    <Height>1</Height>
                </Dimensions>
            </RequestedPackages>
        </Packages>
        <ShipTimestamp>2018-07-18T08:00:00 GMT+0100</ShipTimestamp>
         <Content>NON_DOCUMENTS</Content>
        <PaymentInfo>DAP</PaymentInfo>      
    </RequestedShipment>
      </rat:RateRequest>
   </soapenv:Body>
</soapenv:Envelope>';

try {

    // the soap client accepts options, we specify the soap version
    // The trace option enables tracing of request so faults can be backtraced.
    // The exceptions option is a boolean value defining whether soap errors throw exceptions of type SoapFault. 
    $opts = array(
        'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
        )
    );
    $options = array(
        'encoding' => 'UTF-8',
        'verifypeer' => false,
        'verifyhost' => false,
        'soap_version' => SOAP_1_2,
        'trace' => 1,
        'exceptions' => 1,
        'connection_timeout' => 180,
        'stream_context' => stream_context_create($opts),
        'cache_wsdl' => WSDL_CACHE_NONE,
    );

    // create the soapclient and invoke __doRequest method
    $client = new SoapClient($url, $options);
    $output = $client->__doRequest($xmlrequest, $url, $action, 1);
} catch (SoapFault $fault) {
    echo "<h2>SOAP Fault!</h2><p>";
    echo "FaultCode: {$fault->faultcode} <br/>";
    echo "FaultString: {$fault->faultstring} <br/>";
    echo"</p/>";
}

echo "<h2>WSDL URL: </h2><p>";
echo $url;
echo "</p/>";

echo "<h2>Action: </h2><p>";
echo $action;
echo "</p/>";

echo "<h2>XMLRequest: </h2><p>";
echo $xmlrequest;
echo "</p/>";

if (!isset($output)) {
    echo "<h2>SOAP Fault!</h2><p>";
    echo "FaultCode: {$output->faultcode} <br/>";
    echo "FaultString: {$output->faultstring} <br/>";
} else {
    echo "<h2>Output: </h2><p>";
    file_put_contents('dhl.xml', $output);
    echo $output;
    echo "</p/>";
}

如果manifest.json不存在,则应创建它,如下所示:

  

{          “ css / app.css”:“ build / css / mystyle.b916426ea1d10021f3f17ce8031f93c2.css”,          “ ...”:“ ...”      }

答案 4 :(得分:0)

我通过不使用json_manifest_path解决了这个问题:〜

通过在json_manifest_path前面添加“#”来注释它 并在“资产:”之后添加“〜”

nzCustomRequest

答案 5 :(得分:0)

请记住,该问题被标记为 Symfony4

我在导航栏,网站图标中添加徽标也遇到了同样的问题……

在Symfony4中,没有更多的 app 目录,因此没有 app / config / config.yml ! 在Symfony4中, web 目录已不存在,但有新目录 public

正如@Bijaya所写,只需在公共位置指定 href src 地址

就我而言,我有自己的徽标: myProjectName / public / logo_mom.png

在我的控制器中,我有:

/**
 * @Route("/", name="home")
 */
public function home() {
    return $this->render('blog/home.html.twig', [
        'title' => "Bienvenue",
        'logo_path' => "logo_mom.png",
        'logo_alt' => "logo alt description",
    ]);
}

以及在我的 home.html.twig 中:

{% block body %}
    <h1>{{ title }}</h1>
    <img src={{ logo_path }} alt={{ logo_alt}}>

以及在我的 template / base.html.twig 中:

<link rel="icon" type="image/x-icon" href="favicon.ico" />

因此,对于那些不具备Symfony先前版本知识而发现Symfony4的我来说,很高兴不需要资产功能或任何其他配置!

答案 6 :(得分:0)

我通过不使用 json_manifest_path: ~

解决了这个问题

注释掉json_manifest_path,在它前面添加#并在~之后添加assets:

framework:
    assets: ~
        #json_manifest_path: '%kernel.project_dir%/public/build/manifest.json'