Can't access controller nore action in Zend Framework - php

In my Zend Framework project I have the following base URL
http://domain/~myFolder
If I access it like so, I can access the indexAction with no problem but if I try to access it explicitly like so:
http://domain/~myFolder/index
I can't, nore can I access different controllers or actions.
It always says
/public/index.php was not found on this server.
How can I access the different controllers and actions in my project? Do I need to add a route?

Change the contents of the .htaccess file according to...
From: http://framework.zend.com/manual/1.12/en/zend.application.quick-start.html
You may note that the application environment constant value looks for
an environment variable "APPLICATION_ENV". We recommend setting this
in your web server environment. In Apache, you can set this either in
your vhost definition, or in your .htaccess file. We recommend the
following contents for your public/.htaccess file:
SetEnv APPLICATION_ENV development
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

I was able to access the controller page by putting the controller name after the index.php:
ie: yoursite/public/index.php/ControllerName
It worked in my case.

Related

htaccess - Redirect parent folder PHP script

I have this web api project which is developed by other company. The file structure is:
/project
---/app
---/ApiEndpoint.php
---/public
---/index.php
The DocumentRoot is pointing to /project/public. The index.php is working (http://myapi.com/), however when I try to browse into the api endpoint http://myapi.com/api/endpoint I got 404 error.
How do I configure the .htaccess to rewrite this condition?
/project/public/.htaccess config
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /index.php?path=$1 [NC,L,QSA]
</IfModule>
The current configuration is there to make sure every request go to index.php (that is probably dispatching requests in some way) except for the files that actually are in the public directory (probably needed for static files like images, and such).
If you wrote a php yourself to be directly called by http://myapi.com/api/endpoint, you should put it in /project/app/public/api/endpoint/index.php.
BUT I suspect you should study that application more and understand the current dispatching method, before doing that.

The requested URL /how-do-i-find-the-difference-between-two-dates-using-jquery was not found on this server

I have sub-domain likedemos.testing.com and i would like to get my url like
demos.testing.com/how-do-i-find-the-difference-between-two-dates-using-jquery
I have index.php file in demos.testing.com root folder and i reading the first parameter as
$request_uri = substr($_SERVER["REQUEST_URI"], 1);
When i request site like : demos.testing.com/how-do-i-find-the-difference-between-two-dates-using-jquery it is giving following error :
The requested URL /how-do-i-find-the-difference-between-two-dates-using-jquery was not found on this server.
Please tell me how can rewrite url in .htaccess file.
.htaccess is an Apache specific configuration file, and more over depending on your main Apache configuration, it may or may not even be parsed. To get an answer, you need to properly tell us what Web server you are using (is it Apache? If not .htaccess won't work and you need to write your rewrite rules in some other Web-server specific way). If Apache, do you have AllowOverride set?
Assuming all of this is true (you use Apache and it is set to read .htaccess), try this rewrite rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
This basically rewrites everything which is not a real existing file or directory to index.php.
As I said, depending on your setup, this may not work. If it doesn't, please tell us more about your setup (which server, what PHP SAPI, etc.).

Alias, mod_rewrite or something else for shared php scripts?

I have a number of domain names all pointing to the same server and I'm looking for a way to access a set of scripts in a single location from each domain name as a kind of shared resource.
e.g.
http://abcdeals.com/shared-resource/
http://defdeals.com/shared-resource/
http://hijdeals.com/shared-resource/
all use the same files at /home/zdeals/public_html/shared-resource/
Full details:
The "shared resource" files reside in the following server path
/home/zdeals/public_html/shared-resource/
The multiple domain names e.g. (abcdeals.com, defdeals.com and hijdeals.com) reside in the following server path
/home/abcdeals/public_html/
/home/defdeals/public_html/
/home/hijdeals/public_html/
Ideally I would like to be able to access the shared resource files without redirecting the user by accessing
http://abcdeals.com/shared-resource/
http://defdeals.com/shared-resource/
http://hijdeals.com/shared-resource/
I've been looking at alias and mod_rewrite but to no avail.
Some things I've tried in /home/abcdeals/public_html/.htaccess
AliasMatch ^/shared-resource(.*) /home/zdeals/public_html/shared-resource$1
or
RewriteRule ^shared-resource/(.*) home/zdeals/public_html/shared-resource/$1 [L]
...and many more similar
I either get a 500 internal server error or 404 not found error when accessing http://abcdeals.com/shared-resource/
I also have access to the httpd.conf file if that helps.
The existing .htaccess file is as follows
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
#RewriteBase /
RewriteRule . index.php [PT]
RewriteRule ^(system/(classes|locale|schema|$)) index.php [PT]
I will be eternally grateful for any help on this.
You can use symlinks for this. In linux you can do this by:
ln -s /home/zdeals/public_html/shared-resource/ /home/abcdeals/public_html/shared-resource/
Which creates a symlink /home/abcdeals/public_html/shared-resource/-->/home/zdeals/public_html/shared-resource/

Routing URLs in PHP

I'm working on a web page project. I decided to use Apache, PHP (5.1.7, version imposed by my service provider) and Dwoo (templating) for this purpose.
I want to route URLs to my templates. I'm aware there are many frameworks doing this kind of thing. I'm just wondering if there's a nice way to achieve it without.
I've set up my project as follows:
src/dwoo - Dwoo files
index.php - This should handle routing. Currently it just renders the front page of the site using a template.
templates - Templates that represent actual pages.
There is minimal amount of business logic (no real model). It's all just pretty static pages. Using templates makes maintenance work easier (inheritance ie.).
Any idea how to set up routing in this case? I guess ideally each given URL should route via index.php that then somehow decides which template to render (ie. /category/pagename would map to templates/category/pagename.tpl).
Use mod_rewrite to route everything to a single index.php file. Then check the variable in $_SERVER['REQUEST_URI'] within this file to dispatch to the required handler.
This configuration will enable mod_rewrite, if it's installed:
DirectorySlash Off
Options FollowSymLinks Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [L]
RewriteRule ^.*$ index.php [L]
Like trolskn (+1) describes:
Use mod_rewrite to route everything to a single index.php file. Then check the variable in $_SERVER['REQUEST_URI'] within this file to dispatch to the required handler.
But I found the following .htaccess (placed in the folder with the index.php which should "consume" everything after it) much more helpful:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
Also I would like to note that you might encounter the message
.htaccess: Invalid command 'RewriteEngine', perhaps misspelled
or defined by a module not included in the server configuration
This can easily be solved with sudo a2enmod rewrite && sudo service apache2 restart (source)
You might want to use PEAR's Net_URL_Mapper.

Zend Framework with phpBB: URL problem

I am using zend framework with Apache sever on Ubuntu. When I try my site on localhost, I use following url
for example:
http://test.dev/authentication/login
http://test.dev/student/profile
Where 'authentication' refers to AuthenticationController and 'login' refers to loginAction.
'student' refers to StudentController and 'profile' refers to profileAction.
Question: Now I want to use phpBB forum with my site. I had to use the following URL to start phpBB forum with my website.
http://localhost/test/forum/
Where 'test' is my main project(website) directory. I want to use following URL for phpBB forum.
http://test.dev/forum/
How to configure my project to open phpBB forum using above URL? Should I create a controller?
Thanks
I guess you are using apache mod_rewrite with your ZF application.
The simplest solution is to place a new .htaccess file inside the forum/ directory and turn off the rewrite engine
RewriteEngine Off
This works because Apache first look for the .htaccess file in the requested directory, if it does not find one, he looks in the parents folder and so on until it reaches the public directory. When you request /forum/ he finds the .htaccess file there and turns off the RewriteEngine.
After some time I have found my answer. I just placed my 'forum' folder in 'public' folder and it is working for me without changing my .htaccess file.
Here is my settings:
My directory structure is now like this:
/var/www/test/public/index.php
/var/www/test/public/.htaccess
/var/www/test/public/forum
My httpd.conf entry is like this:
<VirtualHost 127.0.0.1>
ServerName test.dev
DocumentRoot 'C:\wamp\www\test\public'
</VirtualHost>
My .htaccess file is like this(it controls both folder and controller/action URLs):
SetEnv APPLICATION_ENV development
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Last line in above .htaccess file allow me to use my URL without index.php in URL.
After above setting I am able to use following URLs correctly:
http://test.dev/authentication/login
http://test.dev/student/profile
http://test.dev/forum/
In above URLs 'authentication' and 'student' are controllers. 'login' and 'profile' are actions and forum is a directory
Comments are welcome.
Thanks
This will work as well:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(js|ico|gif|jpg|png|css)$ /index.php
If you place phpBB in the public folder the webserver will be able to see it and the 2nd two lines of this code will exclude "real files" and "real folders" from the rewrite which is what you want for the forum folder. Obviously the last time, pushes all framework traffic to the index.php file.

Categories