Frage

i am unsing zend framework. i have a view linked to a controller, home,

so:

application>controllers>HomeController.php

application>views>scripts>home>index.phtml

in the index.phtml page i have a link to an action in the same controller:

application>views>scripts>home>add.phtml

the code for the link is:

<a href="../application/controllers/home/add">add</a>

Not only does this not work but i am sure its not the best way to do this in zend framework.

please note i am using wamp

http://localhost/sites/url/public/home/add

when using:

<?php
    echo '<a href="' . $this->url(array('controller' => 'home', 'action' => 'add')) . '">Add Job</a>';  
?>

I get this error message:

Message: Invalid controller specified (sites)

this is whats in my htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
War es hilfreich?

Lösung

Use the url helper for this:

http://framework.zend.com/manual/en/zend.view.helpers.html

<a href="<?php echo $this->url(array('controller' => 'index', 
                                     'action' => 'index', 
                                     'module' => 'module1')); ?>" title="test">

UPDATE

This is how your .htaccess should like:

SetEnv ENVIRONMENT development

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteCond %{REQUEST_URI} !\.(css|gif|ico|jpg|js|png|swf|txt)$
RewriteRule ^.*$ index.php [NC,L]

Andere Tipps

when using relative and absolute links in ZF, it works pretty much the same as outside of ZF except that all request route through index.php so you are not actually routing to the directory, your just telling the router where the link goes:

<a href="/home/add">add</a>

or

<a href="http://mysite.com/home/add">add</a>

The url helper would be the preferred way to do this and should have worked. In your case I suspect the localhost pathing os creating difficulties with your urls, I know it always did with mine.

Setting up a vhost for each project will make this issue disappear. If you need help with a vhost setup there a number of good resources here on SO.

http://blog.srmklive.com/2010/12/27/how-to-create-virtual-hosts-using-zend-server-ce/

You don't have to refer to the application folder in your links, because that folder can be located outside your htdocs/www/public_html folder.

The link should look like: http://yourdomain.com/home/add or http://localhost/home/add, depending where you installed your application.

The best way to do it in my opinion, is either use the $this->baseUrl() method (works only in views, not controllers), or you can define a constant in bootstrap.php, which holds your baseUrl.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top