karim_fci
karim_fci

Reputation: 1242

Zend Framework 2: basePath() print nothing

I am wondering why basePath() print nothing in layout.phtml. I am using zend skeleton Application. Does virtual host can make this problem? Here is the code snippet and output.

echo $this->headLink(array('rel' => 'shortcut icon', 'type' => 'image/vnd.microsoft.icon', 'href' => $this->basePath() . '/img/favicon.ico'))
                        ->prependStylesheet($this->basePath() . '/css/style.css')
                        ->prependStylesheet($this->basePath() . '/css/bootstrap-theme.min.css')
                        ->prependStylesheet($this->basePath() . '/css/bootstrap.min.css');

HTML Output

        <link href="/css/bootstrap.min.css" media="screen" rel="stylesheet" type="text/css">
<link href="/css/bootstrap-theme.min.css" media="screen" rel="stylesheet" type="text/css">
<link href="/css/style.css" media="screen" rel="stylesheet" type="text/css">
<link href="/img/favicon.ico" rel="shortcut icon" type="image/vnd.microsoft.icon">

How i solve this. Help will highly appreciated. Thanks in advance.

Upvotes: 2

Views: 510

Answers (3)

Bryan P
Bryan P

Reputation: 4202

Try adding this to your controller:

public function baseUrl() {
    $protocol = isset($_SERVER['HTTPS']) ? 'https' : 'http';
    $server = $_SERVER['HTTP_HOST'];
    $port = $_SERVER['SERVER_PORT'] != 80 ? ":{$_SERVER['SERVER_PORT']}" : '';
    $path = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/\\') . '/';

    return "$protocol://$server$port$path";
}

If it still doesn't work, pass that as a variable to your view so that you can use it on your view.

Upvotes: 0

dixromos98
dixromos98

Reputation: 754

Try it like this

    $print = $this->headLink(array('rel' => 'shortcut icon', 'type' => 'image/vnd.microsoft.icon', 'href' => $this->basePath() . '/img/favicon.ico'))
                                    ->prependStylesheet($this->basePath() . '/css/style.css')
                                    ->prependStylesheet($this->basePath() . '/css/bootstrap-theme.min.css')
                                    ->prependStylesheet($this->basePath() . '/css/bootstrap.min.css');


>    echo $print;

or as a temporary solution.

<link href="<?php echo $this->basePath(); ?>/css/style.css" rel="stylesheet" type="text/css"/>

Upvotes: 0

edigu
edigu

Reputation: 10099

Instead of basePath() you should try to use serverUrl() view helper.

Example:

$this->prependStylesheet($this->serverUrl('/css/style.css'))
     ->prependStylesheet($this->serverUrl('/css/bootstrap-theme.min.css'))
     ->prependStylesheet($this->serverUrl('/css/bootstrap.min.css'));

Upvotes: 1

Related Questions