Search code examples
javascriptphpzend-framework

javascript script source won't be found by browser zendframework


I'm trying to import a script into my .phtml file.

<body>

<SCRIPT type="text/javascript" src="script.js"></SCRIPT>

</body>

But the browser console returns me an error message: error with loading source "http://myhost/a/script.js"

My project structure is:

app/module/someName/view/a/b/script.js

app/module/someName/view/a/b/index.phtml

I can include my php files laying in the same directory into my index.phtml. But somehow when i want to include the java script code I get an error.

My guess was i use the InvokableFactory with my controller. Is there some correlation?

module.config.php

'router' => [
        'routes' => [
            'a' => [
                'type' => Literal::class,
                'may_terminate' => true,
                'options' => [
                    'route' => '/a',
                    'constraints' => [
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    ],
                    'defaults' => [
                        'controller' => MonitoringController::class,
                        'action' => 'index',
                    ],
                ],
                'child_routes' => [


            'b' => [
                'type'    => Segment::class,
                'options' => [
                    'route' => '/b[/:action[/:id]]',
                    'constraints' => [
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'     => '[0-9]+',
                    ],
                    'defaults' => [
                        'controller' => DashboardController::class,
                        'action'     => 'index',
                    ],
                ],
            ],

Solution

  • I imported the script with php and now it works good.

    $this->inlineScript()->appendFile($this->basePath('pathTo/javaScriptFile.js'));