Zend_Controller_Router_Route causing an 404 error - php

It's probably late and I have missed this off by a long shot.
I am trying to create a cleaner url structure; so rather than having
/index/about
/index/news
I have
/about
/news
I came across a post on this site which used the following:
public function _initCustomRoute()
{
$router = Zend_Controller_Front::getInstance()->getRouter();
$route = new Zend_Controller_Router_Route(':action', array(
'module' => 'default',
'controller' => 'index',
'action' => 'index'
));
$router->addRoute('default', $route);
}
It rewrites the url within my navigation. So I have created the relevant action and view (tested without the custom route) but I am getting:
Not Found
The requested URL /path/to/public/index.php was not found on this server.
I assume this is a thing that apache does on a windows file system by not adding the drive letter.
I've not touched the .htaccess file.
Any ideas?

Found out why! You must use virtual hosts rather than an alias as I was using.
Full guide here: http://blog.ryantan.net/2010/03/setting-up-wamp-for-zend-framework-projects/

Related

phalcon router not working

I try to use a router with phalcon. This is how it is included in index.php right after registering the 'events manager':
$di->set('router', function(){
require __DIR__.'/../app/config/routes.php';
return $router;
});
and this is how the routes.php looks like:
<?php
$router = new Phalcon\Mvc\Router(false);
$router->add("/", array(
'controller' => 'index',
'action' => 'index'
));
$router->add("/topics", array(
'controller' => 'wurst',
'action' => 'index'
));
$router->handle();
return $router;
The website reacts as if the router was not existent. /topics and topics say this:
TopicsController handler class cannot be loaded
and I also cannot use a die("test"); function inside routes.php . nothing happens.
I also tried to activate it without a separate file, but the result was the same :(
(The sample web-application INVO was used as starting point for my site )
$router->setUriSource(Router::URI_SOURCE_SERVER_REQUEST_URI); will use default $_SERVER['REQUEST_URI']
If your index/index action is working when you access domain.com/index.php, check that you are using proper uri source, if using nginx or php built-in server you might have some problems with routing and $_GET['_uri'] which phalcon use for handling uris.
can find more about it on phalcon router documentation about uri sources -> http://docs.phalconphp.com/en/latest/reference/routing.html#uri-sources
now it seems to work:
Action 'route404' was not found on handler 'index'
the problem was , that I put the function to set the router in index.php within "set dispatcher function". ..did not see the closing braces.

CakePHP Plugin - how to setup Apache Rewrite for vanity URL

Been trying to figure this out for a few days now with no joy. Here's my setup: I have a CakePHP install in
/home/user/tools/cakephp
and a plugin at
/home/user/tools/cakephp/app/Plugin/MyPlugin
The Apache server setup is such that I've set the DocRoot to /home/user/tools, so browsing to
http://myserver.com/cakephp/my_plugin
works fine, but now my client wants to set it up so that
http://myserver.com/product-name
serves up the CakePHP plugin, and all subsequent routes are honoured. Has anyone had any experience setting something like this up? Has to be Apache, unfortunately, and can be done with a mixture of config/.htaccess (clients constraints).
Thanks
Stephen
You are saying you have to do this via the Apache config or a .htaccess file, why? A plugin can have either his own Config/routes.php or you could configure the routing in your app wide app/Config/routes.php file. We are doing the same for a plugin we are using in our application.
What we did:
In the app wide routes file (app/Config/routes.php) we set a variable we use as our base url for accessing the plugin. We set it in a variable so we can easily switch when we get collisions with other controllers or plugins, so we want to keep that flexibility by doing the following:
# set the webroot for the plugin, for ajax calls and the sake of usability
$MyPluginBase = '/product-name';
# And we have this set just in case we need it somewhere in our application
Configure::write('MyPlugin.base', $MyPluginBase);
Then we configure our custom routes:
Router::connect($MyPluginBase . '/:name', array(
'plugin' => 'my_plugin',
'controller' => 'my_plugin_products',
'action' => 'products'
));
Router::connect($MyPluginBase . '/some/other/url/*', array(
'plugin' => 'my_plugin',
'controller' => 'my_plugin_some_controller',
'action' => 'whatever'
));
Now we can access the pugin via the 'product-name' url.
But, when you simply need a controller/action wide solution for this, you could accomplish this by using the following two routes:
Router::connect($MyPluginBase . '/:controller/:action/*', array(
'plugin' => 'my_plugin'
));
Router::connect($MyPluginBase . '/*', array(
'plugin' => 'my_plugin',
'controller' => 'my_plugin_main_controller'
));
Please note that the order of Router::connect methods matters!
ps. After reading the question again, I saw you have set your DocumentRoot wrong for production. Consult the following page in the cookbook for clarification: http://book.cakephp.org/2.0/en/installation.html#production
Adding the following line to the htaccess file should work
RedirectMatch 301 cake/my_plugin(.*) /product-name$1
The above would resolve:
http://myserver.com/cakephp/my_plugin to http://myserver.com/product-name
http://myserver.com/cakephp/my_plugin/somelink to http://myserver.com/product-name/somelink
When you say "all subsequent routes are honoured", I assume you mean that http://myserver.com/product-name/foo/bar will work the same as http://myserver.com/cakephp/my_plugin/foo/bar.
If that is the case, and you have mod_aliasinstalled, all you need to do is provide an Alias directive in httpd.conf:
Alias /product-name /cakephp/my_plugin
This should be completely transparent to CakePHP; it will be unaware that this mapping is happening.
If you want to prevent direct requests to http://myserver.com/cakephp/..., you could also add an external redirect:
Redirect 301 /cakephp/my_plugin /product-name
(from http://httpd.apache.org/docs/current/mod/mod_alias.html#alias)

php - Zend_Controller_Router_Route_Regex not working

I wanna match an url like this http://localhost/sunnetwap/1-hot and rewrite into this
http://localhost/sunnetwap/?c=1&s=hot.
In my Bootstrap and and _initRoutes function i wrote:
$frontController = Zend_Controller_Front::getInstance();
$route = new Zend_Controller_Router_Route_Regex(
"([0-9]*)-([a-z]*)",
array(
'module' => 'default',
'controller' => 'index',
'action' => 'index'
),
array(
1 => 'c',
2 => 's'
));
$frontController->getRouter()->addRoute('home',$route);
but it doesn't work. Can you help me?
The issue is probably related to the base url of your app.
It is possible that you have deployed your app at the base url http://localhost/. In this case, your routes would need to include the sunnetwap portion.
But I'm guessing that your app is deployed to http://localhost/sunnetwap/. In that case, your route is fine, but it's likely that you either have a sunnetwap alias on localhost or that the whole app is dropped into into your <docroot>/sunnetapp and that you have moved contents of the public folder up a level with corresponding changes to .htaccess. Both of these will potentially have impact on the routing.
See this answer for some ideas on how to deploy to a sub-directory when you can't map a virtual host to your app.

Routes on zend modules

I want to make a kind of alias for my modules:
Is there a way to make
http://www.example.com/news point to
/modules/news/controller/news/->action
index.
And if i go to
http://www.example.com/news/show it
automaticly points to
/modules/news/controller/news/ ->
action show
Or do I have to make a route for every single action I make in this module?
If I don't make a route my links will look like:
http://www.example.com/news/news/show and http://www.example.com/news/news/show.
P.s i'm using Zend 1.10.6
Thanx in advance!
I guess it will be something like that:
$route = new Zend_Controller_Router_Route(
'news',
array('module' => 'news', 'controller' => 'news', 'action' => 'index'));
$router->addRoute('news', $route);
where first arg 'news' for Zend_Controller_Router_Route is the url http://www.example.com/news that will route to http://www.example.com/news/news/index
salu2
In your config file you must enable modules by putting this in your config file
resources.modules[] =
And it should by default route correctly in your application.
You will have to define a route for each URL you want routed in a non-standard way.

Routing works on localhost, but not on live server

When I access my site on MAMP like so, it works great
localhost/site/about-us/
When I upload it to my remote server, and access it like this
http://www.server.com/site/about-us/
all requests go back to the 'default' set up in bootstrap.php.
Here is my route setting.
Route::set('default', '(<page>)')
->defaults(array(
'page' => 'home',
'controller' => 'page',
'action' => 'index',
));
The problem is, whenever it gets uploaded to my server, any request like /about-us/ is always defaulting to home as specified when setting the route. If I change that default to 'about-us', every page goes to 'about us'.
Does anyone know what may be causing this? Thanks
UPDATE
Here is a hack that works, but is sure ugly as hell. Still I'd prefer to know why it doesn't work as per expected.
// Hack because I can not get it to go to anything except 'default' below...
$uri = $_SERVER['REQUEST_URI'];
$uri = str_replace(url::base(), '', $uri);
$page = trim($uri, '/');
if ( ! $page) $page = 'home';
Route::set('default', '(<page>)')
->defaults(array(
'page' => $page,
'controller' => 'page',
'action' => 'index',
));
Your code is basically a catch all route (it's being matched for all requests). You should restrict it like so.
Route::set('static', '(<page>)', array('page' => 'about-us'))
->defaults(array(
'controller' => 'page',
'action' => 'index',
));
The 3rd parameter is a regular expression which defines what the route should match.
That route will route everything matched in the regular expression to the page controller and its index action.
You can then use $page = $this->request->param('page'); in your action.
Are you not mistaking $page for $action?
If I try this, it works just fine. Here's my controllers action method:
public function action_index($page = NULL)
{
var_dump($page);
}
If I browse to
localhost/site/blup
I see see a nice
string(4) "blup"
being echo'd. I have the default route setup identical to yours.
It sounds like Kohana's auto-detection of the URL isn't working for your server setup... What web server is it failing on?
You could alter the Request::instance()->execute()... line in the bootstrap to start with:
Request::instance($_SERVER['REQUEST_URI'])->execute()...
That will ensure it uses the correct URI..
That being said ... as The Pixel Developer says, your route looks.. odd .. to me ;)
But - since it works on MAMP - The route is likely not the issue.

Categories