Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Images don't show in view when accessed by controller.

Hello,

I'm working on a web app with phalcon since april, and encoutering my first unsolvable problem :

I've got a simple html page located at /public/jeux/[jeu_name]/perdu/perdu.phtml. It contains some <img src="./header_mobile"> with relative paths to images.

All goes well when I load the phtml file directly, with full path to the file in browser.

BUT, when I load this view from my dedicated controller :

    public function indexAction() {
        $this->persistent->id_jeu=$_GET['id'];
        $jeuDir=Jeu::findFirstByid($this->persistent->id_jeu)->directory;
        $this->view->setViewsDir('/var/www/html/game-generator/public/'.$jeuDir);
        $this->dispatcher->forward(
            array(
                "action" => "perdu"
            ));
    }

    public function formAction(){
        $this->view->setMainView('form/form');
    }

    public function perduAction(){
        $this->view->setMainView('perdu/perdu');
    }

Images don't show, even if Firebug's network pannels says '200 OK' about images GET requests.

I really need help on this, can't figure out why this isn't working.. halp ! Can do screenshots if needed, or if I wasn't clear in my explanations (frenchie inside, sry ^^ )

Thanks !

edited May '16

The problem is that URL base changes per page (/, /index/, /product/, /product/view/, etc).

Relative image URLs won't fly this way, either make them absolute (I'd recommend this), or wrap the image path with {{ url("path-to-img") }}. You are getting 200 OK code because the http server still resolves the request, probably to index/index which is text/html (you can check that with the network debug tab in your browser).



833

With absolute paths, I get nothing but 404 not found (triple checked the path), even if I direct access the html file.

How should I wrap the path ? <img src={{ url("path-to-img") }}> ?



42.3k
Accepted
answer

Absolute paths as in OS or Web absolute path?

Lets say this is your project root: /var/www/html/game-generator/ Document root (web server root): /var/www/html/game-generator/public/ Image to be served: /var/www/html/game-generator/public/jeux/[jeu_name]/perdu/header_mobile.png

To view this image, you'd have to use this absolute url: /jeux/[jeu_name]/perdu/header_mobile.png (substitute [jeu_name] ofc) If you type this in your browser and get 404 error, then you have some problem with your web server, not Phalcon



833

"Absolute paths as in OS or Web absolute path?" ==> d.. didn't knew there was a difference, I never use absolute paths ^^ Will keep this in mind !

It works awesomely well with "jeux/..." (without the first "/" that u purposed)

Thanks a lot Mr Bencz ! :)



21.3k

What about

{{ image("src":"/img/image.jpg") }}

-> this image should be in public/img/image.jpg



833
edited Jun '16

Hello again, I'm still encoutering little issue with my images :

public function indexAction() {
    return $this->dispatcher->forward(array("action" => "form"));
}

public function gameAction(){
    return $this->dispatcher->forward(array("action" => "win"));
}

public function formAction(){
    $this->view->setViewsDir($this->persistent->jeuDir);
    $this->view->pick('form/form');
}

public function winAction(){
    $this->view->setViewsDir($this->persistent->jeuDir);
    $this->view->pick('win/win');   
}

First, my indexAction fw to formAction and pick form view ==> Images OK. Then, my form submit to gameAction, fw to winAction and pick win view ==> Images KO :(

BUT, If fw to winAction from indexAction, win view shows up with images ! OR, if if fw to formAction from gameAction, form view is loaded without images..

I think it's about url : gameAction adds /game to the current url, is there a way to prevent this ?



833
edited Jun '16

Hi, bumping a bit this topic that's lost in the depths (hope this is allowed). I'm in a real need of help and a bit hurry ; these images are driving me mad.

I made a bunch of php var dumps to try to figure out what's going on with routes, urls, paths resolvings but I really lack knowledge around there.

My images got these type of paths : src="jeux/jeu/form/header_mobile"

From the view 'form', I have these dumps :

jeuDIr : jeux/jeu/

SCRIPT NAME : /game-generator/public/index.php

REQUEST URI : /game-generator/Golden?id=267&ticket=22

__DIR__ from controller: /var/www/html/game-generator/app/controllers

__DIR__ from view: /var/www/html/game-generator/public/jeux/jeu/form

from the view 'gagne', these :

jeuDIr : jeux/jeu/

SCRIPT NAME : /game-generator/public/index.php

REQUEST URI : /game-generator/golden/jeu

__DIR__ : /var/www/html/game-generator/app/controllers

__DIR__ from view : /var/www/html/game-generator/public/jeux/jeu/gagne

The only remarquable difference is the request URI, wich I suppose is somehow used for path resolving..

For some reason I can't use absolute paths for my images, only get 404 not found.

I'll just add that I'm using default .htaccess as described here : https://docs.phalconphp.com/en/latest/reference/apache.html

Thanks in advance for any help !