2

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]
tereško
  • 58,060
  • 25
  • 98
  • 150
RSM
  • 14,540
  • 34
  • 97
  • 144
  • Could you give the url of your homepage (i.e., what is in your browser's location bar) and url given by `$this->url()` (i.e., what's in this a href="")? – raina77ow Sep 01 '12 at 22:57
  • I mean you probably have to add this line - `resources.frontController.baseUrl = "/sites/url/public"` to your `config.ini` file; still I wonder is it the right path. – raina77ow Sep 01 '12 at 23:01
  • i havent set up a.htacces file and i acces my site by clicking on my folder, clicking public, then clicking index file – RSM Sep 01 '12 at 23:03
  • But you still do open your website in your web-browser, right? What's in its upper bar then? And what's in that link (press `Ctrl-U` to view the source). – raina77ow Sep 01 '12 at 23:05
  • The htacces you posted is the stock implementation the ZF sets up so it will work for most situations. – RockyFord Sep 02 '12 at 06:54

3 Answers3

3

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]
Keyne Viana
  • 6,194
  • 2
  • 24
  • 55
  • In which directory is your index.php within the .htaccess? – Keyne Viana Sep 01 '12 at 22:59
  • @RyanMurphy And where's your index.php? You should have the .htaccess too. Man the thing is you need both index.php and .htaccess on this folder: /sites/url/public/ – Keyne Viana Sep 01 '12 at 23:05
  • i have the index.php in the public folder not a htaccess – RSM Sep 01 '12 at 23:06
  • @RyanMurphy Is there `RewriteBase` line? What's in it? – raina77ow Sep 01 '12 at 23:09
  • @RyanMurphy If you access this link http://localhost/sites/url/public/home/add removing the `$this->url()` part it works? If so, your `.htaccess` is working – Keyne Viana Sep 01 '12 at 23:36
  • just redirects to a homepage of a different site in my sites folder – RSM Sep 01 '12 at 23:45
  • @RyanMurphy Are you sure there isn't any index.php/.htacess before the your app public folder? You should at least be abble to access http://localhost/sites/url/public/index.php Are you able to do so? – Keyne Viana Sep 01 '12 at 23:54
  • yes i am. but when i add a link to another page and click on that link the situation described in the question happens – RSM Sep 01 '12 at 23:55
  • 1
    @RyanMurphy If you're able to access both http://localhost/sites/url/public/index.php and http://localhost/sites/url/public/ it's working. If the .htaccess is properly set then you should be able to acess http://localhost/sites/url/public/home/add Are you sure this last link is not working? You need to fix this step first before going to the url part on the view – Keyne Viana Sep 02 '12 at 00:01
2

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/

RockyFord
  • 8,529
  • 1
  • 15
  • 21
0

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.

Eduard Luca
  • 6,514
  • 16
  • 85
  • 137