Zend Framework: Page not found - zend-framework

I have developed a web application with Zend Framework which root is http://www.demo31.com/validacion/demo31/ but when I call that url I've got the next error:
Page not found
Request Parameters:
array (
'controller' => 'validacion',
'action' => 'demo31',
'module' => 'default',
)
I want that the values of array would be next:
array (
'controller' => 'index',
'action' => 'index',
'module' => 'default',
)
And my .htaccess is correct.
So, what do I have to do what I want?

Zend framework normally operates as per routes. If a particular URL is not reaching your code, then you have to configure routes to do that.
$router = $front -> getRouter();
$routePage = new Zend_Controller_Router_Route('/:controller/:action', array(
/* ^ Things to notice
Only two parameters are
asked from the route */
'controller' => 'default',
'action' => 'index',
'module' => 'default' //Predefine the module as `default
));
$router -> addRoute('default', $routePage);

By default, ZF assumes that the app is located in the root of the domain so that's why it treats validacion as a controller.
Zend Framework in a subfolder

Related

zend framework 1.11.11 routing

http://xxxxxx.xxx/man/pant/man-yellow-box-pant
$router = $ctrl->getRouter(); // returns a rewrite router by default
$router->addRoute(
'location',
new Zend_Controller_Router_Route(
':category/:subcategory/:productname',
array(
'module' => 'default',
'controller' => 'product',
'action' => 'index',
'id' => 'id',
'name' => 'p_name'
)
)
);
above this code is not working..
$routers = $ctrl->getRouter(); // returns a rewrite router by default
$routers->addRoute(
'location',
new Zend_Controller_Router_Route(
':uname',
array(
'module' => 'default',
'controller' => 'user',
'action' => 'index',
'id' => 'uid',
'name' => 'u_name'
)
)
);
above is working...........
please suggest me why not working when both code write on same page, same project..
Your routes need to have a unique name. By calling them both 'location', the second one you add replaces the first.
I know writing mistake, if we change location to other then also not working because i have more URL condition and condition is we not use on module/controller/function name on URL, according to category or function we use to name On URL.
I have 4 module and 15 controller with as per more function and need to all URL make to SEO friendly so i did, if i change to location with username, he not working,and
$routers = $ctrl->getRouter(); // returns a rewrite router by default
$routers->addRoute('category',
new Zend_Controller_Router_Route(
':category/:subcategory/:productname',
array(
'module' => 'default',
'controller' => 'product',
'action' => 'index',
'id' => 'id',
'name' => 'p_name')));
Category and Subcategory system is not working.
how to set right path way as per my condition and not through 404 error
URL get Like http://testing.com/man/pant/man-yellow-box-pant

Using Zend translate in Bootstrap.php

I have several routes defined in Zend's Bootstrap.php that I want to translate with Zend's translate function:
$trans = new Zend_View_Helper_Translate();
$router->addRoute(
'myroute',
new Zend_Controller_Router_Route(':lang/'.$trans->translate('mytitle').'/',
array(
'module' => 'default',
'controller' => 'index',
'action' => 'statistics'
)
)
);
The helper itself seems to work (doesn't throw an exception or error) but always returns mytitle instead of the actual translation which is defined in the language file (I checked - the language files work in the view).
How can I get the translate function to work in the Bootstrap.php file?
Remember that you needs to load translations before you use it.
Load routing translations like:
$router->setDefaultTranslator($yourTranslator);
Best how to use routing translations is adding '#' before a word you want to translate f.e.:
$router->addRoute(
'myroute',
new Zend_Controller_Router_Route(':lang/#mytitle/',
array(
'module' => 'default',
'controller' => 'index',
'action' => 'statistics'
)
)
);

Zend Framework: Router

That is my two routers:
->addRoute('viewTextMaterial', new Zend_Controller_Router_Route(':mCat/:mCatSub/:mId/:mTitle', array('controller' => 'index', 'action' => 'viewtextmaterial')))
->addRoute('viewNews', new Zend_Controller_Router_Route(':nCat/:nId/:nTitle/:page', array('controller' => 'index', 'action' => 'viewnews')))
In index.phtml file I add this:
Test
Exp. for viewnews URL:
some text
But why, when I click a href, it redirect me to 'viewnews'?
In my experience(which is not very great :) )
I think when you use the colon in front of a name, when you are defining a router
i.e like
'/:mCat/:mCatSub/:mId/:mTitle',
array(
'controller' => 'index',
'action' => 'viewtextmaterial'
)
What you are telling the router to do is to route any url, which follows the above format('/:mCat/:mCatSub/:mId/:mTitle'), to be routed to the controller/action you mentioned there. eg.
someController/action/x/y
or
anoCont/act/a/b
would be routed to the same controller/action.
So in your case what you are doing is you are defining two routers with same options(which creates ambiguity), and by default the second defined route is used(Bottom to top matching).
you can use something like this
'/test/:mCatSub/:mId/:mTitle',
array(
'controller' => 'index',
'action' => 'viewtextmaterial'
)
so anything that starts with 'test' as controller(in the url) would now be routed to your desired controller/view.
Hope it works.. :) (If it doesn't please enlighten me :) )

Regex Routing - rule not being found

I'm defining regex routes for cleaning up my URLS. The idea is that all pages added by the user will be use the URL www.example.com/page-slug rather than using the actual controller, www.example.com/userpages/page-slug. Other pages will follow the standard module:controller:action routing scheme.
I'm trying to aceive this using router precedence.
I have defined the scheme below..
class Default_Bootstrap extends Zend_Application_Module_Bootstrap{
protected function _initRoute() {
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter(); // returns a rewrite router by default
$route['index'] = new Zend_Controller_Router_Route_Regex(
'/',
array(
'module' => 'default',
'controller' => 'index',
'action' => 'index'
)
);
$route['contact'] = new Zend_Controller_Router_Route_Regex(
'contact/(\d+)',
array(
'module' => 'default',
'controller' => 'contact',
'action' => 'index'
)
);
$route['research'] = new Zend_Controller_Router_Route_Regex(
'research/(\d+)',
array(
'module' => 'default',
'controller' => 'research',
'action' => 'index'
)
);
$route['account'] = new Zend_Controller_Router_Route_Regex(
'account/(\d+)',
array(
'module' => 'default',
'controller' => 'account',
'action' => 'index'
)
);
$route['userpages'] = new Zend_Controller_Router_Route_Regex(
'/(.+)',
array(
'module' => 'default',
'controller' => 'userpages',
'action' => 'index'
),
array(
'slug' => 1
),
'%s'
);
$router->addRoute('userpages', $route['userpages']);
$router->addRoute('contact', $route['contact']);
$router->addRoute('research', $route['research']);
$router->addRoute('account', $route['account']);
$router->addRoute('index', $route['index']);
}
}
Things are generally working OK with the router precedence ensuring that index/account/research/contact pages are picking up the correct controller. However, when attempting to go to a URL covered by the "userpages" route e.g. "about-us", final catch all route is not being found resulting in...
Message: Invalid controller specified (about-us)
.
.
.
Request Parameters:
array (
'controller' => 'about-us',
'action' => 'index',
'module' => 'default',
)
Any idea where I'm going wrong here? It seems to me that the regex is correct "/(.+)" should be catching eveything that is not the index page.
EDIT: #phatfingers, OK you're right, I've edited "\d+" to ".+" to catch one or more of any character. The problem persists. In fact before changing the regex, I tried the URL www.example.com/52, and got the same error - "Invalid controller specified (52)". After the change - with code as per the edited snippet above, the rule is still failing to find any matches.
Drop the forward slash in the 'userpages' regex, i.e. just ('.+)
The quote is straight from the manual Zend Router and Router_Regex but afaik it also applies to all the routes.
Note: Leading and trailing slashes are trimmed from the URL in the
Router prior to a match. As a result, matching the URL
http://domain.com/foo/bar/, would involve a regex of foo/bar, and not
/foo/bar.

Pretty Zend Framework urls

I would like to have my urls like this:
/index
/contact
/articles
/articles/selection
...
Instead of:
/index/index
/index/contact
/articles/index
/articles/selection
...
Basically I have only one controller. Which solution is the best to perform this? (controllers and redirections, ZF routing, url rewriting, something else?)
Have a look at the documentation. The behaviour you want is configured as default in the default router:
http://framework.zend.com/manual/en/zend.controller.router.html
if the first param do not maps a module name, it will search for a controller and if this fails too, it is looking for an action in your IndexController.
Did you tried calling your url's like you want to?
What happens if you navigate to /index? Should be the same like /index/index
use zend routing :
$router = Zend_Controller_Front::getInstance()->getRouter();
$route_index = new Zend_Controller_Router_Route(':action', array(
'module' => 'default',
'controller' => 'index',
'action' => 'index'
));
$router->addRoute('route_index', $route_index );
$route_articles = new Zend_Controller_Router_Route('articles/:action', array(
'module' => 'default',
'controller' => 'articles',
'action' => 'index'
));
$router->addRoute('route_articles ', $route_articles );