strange urls in Zend Framework
The Zend project creates URLs like: http://localhost/z开发者_StackOverflowftest/public/index/add
How can I have a URL like http://localhost/zftest/add.php
Thanks.
First off, you seem to be after getting rid of the "public" part. There are two ways to it.
First, the .htaccess. In the project's root directory, create two files: index.php
and .htaccess
, with the following contents:
.htaccess:
RewriteEngine On
RewriteRule .* index.php
index.php:
<?php
define('RUNNING_FROM_ROOT', true);
include 'public/index.php';
And there you have it, no "public" in the URL.
The other way to achieve it is by setting up a domain. You'll need to modify two files: hosts
and httpd-vhosts.conf
. The hosts file is located at /etc/hosts
under Linux or in C:\Windows\System32\drivers\etc\hosts
under Windows. Both need root level access. Just add the line
127.0.0.1 zftest.local
Then find your vhosts file. I'm using XAMPP, so here are the paths to it for XAMPP: C:\xampp\apache\conf\extra\httpd-vhosts.conf
under Windows, /opt/lampp/etc/extra/httpd-vhosts.conf
. Just add something like this:
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /opt/lampp/htdocs/zftest/public
ServerName zftest.local
ServerAlias zftest.local
SetEnv APPLICATION_ENV development
SetEnv APPLICATION_DOMAIN zftest.local
</VirtualHost>
Now, when you go to zftest.local
(choose any name you wish, of course), you'll see your front page, no "public" part in the URL.
As for the other part of the question, you need a custom router. Setting up a router is really easy and you can find lots of information about on the internet. Some prefer to do it in the bootstrap, but I believe the easiest way is to do it in the application.ini file. Here's a sample router:
resources.router.routes.getbyroute.type = "Zend_Controller_Router_Route_Regex"
resources.router.routes.getbyroute.route = "(.+)\.php"
resources.router.routes.getbyroute.defaults.controller = index
resources.router.routes.getbyroute.defaults.action = index
resources.router.routes.getbyroute.map.1 = action
resources.router.routes.getbyroute.reverse = "%s.php"
This should take care of your route (in theory, I haven't tested it for correctness, hehe). Investigate routers though, as you'll probably want something more generic than this (this one won't handle other controllers than index
).
I hope this helps.
I don't see the problem with the default Zend routes as that style of URL mapping is considered a good practice (SEO friendly, clear mapping from URLs to modules, controllers and actions), but if you want to change the routing you should read up on the Zend Controller Router Documentation. Then your mapping might e.g. look like this:
<?php
$router = $frontController->getRouter();
$router->addRoute(
'add',
new Zend_Controller_Router_Route('add.php', array('controller' => 'index', 'action' => 'add'))
);
?>
This is normally accomplished by using Virtual Host document root, or more commonly by dropping in an .htacess file to redirect all requests to your /public/index.php.
See http://framework.zend.com/wiki/display/ZFDEV/Configuring+Your+URL+Rewriter for more details.
精彩评论