Zend Controller all actions to one action - php

I have categorie Controller, with index action
I need that when I enter address .com/categorie/education
it uses index action with parameter education .com/categorie/index/education
or redirect all actions to index ??

How are your routes setup? Given the lack of information provided I have to assume that you are using an .ini file, this is how it would work:
; CATEGORIE
categorie.type = "Zend_Controller_Router_Route_Static"
categorie.route = "categorie"
categorie.defaults.controller = categorie
categorie.defaults.action = index
categorie_view.route = "categorie/:slug"
categorie_view.defaults.controller = category
categorie_view.defaults.action = view
Note, I did change it up a bit, as the index action, imo, should list all categories. This way you have a viewAction to list the individual categorie.
To have it as you requested I believe it would be something like:
; CATEGORIE
categorie.route = "categorie/:slug"
categorie.defaults.controller = categorie
categorie.defaults.action = index

Related

Magento seo friendly url in custom module

I would like to create porfolio module, but I need it to be seo friendly, so url should look like:
example.com/portfolio/projectid
where portfolio - module, so it will be index action
projectid - based on field alias
its easy to create example.com/portfolio but how to create controller, or detect what is after?
The same URL hierarchy is in products, so it's category/product
Anybody have idea how to do it?
Below code works for item/id. you can change with your requirement.
$model = Mage::getModel('items/items');
/*Rewrite */
$isSystem = 0; // set 0 for custom url as we have created custom for profile extension
$_itemId = $model->getModuleItemId();//module_item_id field
$_itemName = $model->getTitle();//title field
$_itemName = strtolower(str_replace(" ", "", $_itemName));
// save profile view url rewrite
$viewIdPath = 'item/id'.'/'.$_itemId;
$viewRequestPath = 'items/'.$_itemName;
$viewTargetPath = 'items/index/item/id/'.$_itemId;//controller is itemsController.php, Action is itemAction()
$_coreUrlRewrite = Mage::getModel('core/url_rewrite');
$_coreUrlRewrite->load($viewIdPath, 'id_path'); // check if item path already saved? If yes, $_coreUrlRewrite will contain existing data.
$_coreUrlRewrite->setStoreId($_storeId)
->setIdPath($viewIdPath)
->setRequestPath($viewRequestPath)
->setTargetPath($viewTargetPath)
->setIsSystem($isSystem)
->save();
if(isset($viewRequestPath)){
$model->setUrl($viewRequestPath);
$model->save();
}
/*Rewrite End*/
You can find more info here & here

How to remove action part in zend framework URLs

I`m using zend framework and my urls are like this :
http://target.net/reward/index/year/2012/month/11
the url shows that I'm in reward controller and in index action.The rest is my parameters.The problem is that I'm using index action in whole program and I want to remove that part from URL to make it sth like this :
http://target.net/reward/year/2012/month/11
But the year part is mistaken with action part.Is there any way ?!!!
Thanks in advance
Have a look at routes. With routes, you can redirect any URL-format to the controller/action you specify. For example, in a .ini config file, this will do what you want:
routes.myroute.route = "reward/year/:myyear/month/:mymonth"
routes.myroute.defaults.controller = reward
routes.myroute.defaults.action = index
routes.myroute.defaults.myyear = 2012
routes.myroute.defaults.mymonth = 11
routes.myroute.reqs.myyear = "\d+"
routes.myroute.reqs.mymonth = "\d+"
First you define the format the URL should match. Words starting with a colon : are variables. After that you define the defaults and any requirements on the parameters.
you can use controller_plugin to control url .
as you want create a plugin file (/library/plugins/controllers/myplugin.php).
then with preDispatch() method you can get requested url elements and then customize that for your controllers .
myplugin.php
class plugins_controllers_pages extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$int = 0;
$params = $this->getRequest()->getParams();
if($params['action'] != 'index' ) AND !$int)
{
$int++;
$request->setControllerName($params['controller']);
$request->setActionName('index');
$request->setParams(array('parameter' => $params['action']));
$this->postDispatch($request);
}
}
}

How to createe a URL string based on a named route and keep parameters

I need to create urls for category switching, switching category should reset page to 1st and change cat name but keep rest of url params.
Example of url:
http://example.com/cat/fruits/page/3/q/testquery/s/date/t/kcal/mine/26/maxe/844/minb/9/mint/4/maxt/93/minw/7/minbl/6/maxbl/96
Urls can contain many different params but category name and page should be always first, category without page should work too and show 1st page.
Currently I have defined two routes and I'm using Zend's url helper with named route but it resets params and as end resuls I have /cat/cat_name/page/1 url.
$category_url = $view->url(array('category'=>$list_item['url'],'page'=>1),'category',FALSE)
resources.router.routes.category_main.route = "/cat/:category/*"
resources.router.routes.category_main.defaults.module = "ilewazy"
resources.router.routes.category_main.defaults.controller = "index"
resources.router.routes.category_main.defaults.action = "results"
resources.router.routes.category_main.defaults.category =
resources.router.routes.category_main.defaults.page = 1
resources.router.routes.category.route = "/cat/:category/page/:page/*"
resources.router.routes.category.defaults.module = "ilewazy"
resources.router.routes.category.defaults.controller = "index"
resources.router.routes.category.defaults.action = "results"
resources.router.routes.category.defaults.category =
resources.router.routes.category.defaults.page = 1
Do I need to create custom method for assembling url in this case?
Things I would try:
Add module, controller and action params in $urlOptions (it's the
first array you pass to the view helper)
instead of 'category' route name try null and see what that does for
you.
Try removing this line
"resources.router.routes.category.defaults.category = "
Please specify what version are of zf are you using.
Solution is very simple, one just have to pass all current request params to url helper
(and optionaly overwrite/add some of them, page and category in my case), last variable is set to FALSE to prevent route resetting.
$view->url(
array_merge(
$params,
array('category' => $list_item['url'],
'page' => 1)
),
'category_main',
FALSE
);

Make SEO sensitive URL (avoid id) Zend framework

i have url like this :
http://quickstart.local/public/category1/product2
and in url (category1/product2) numbers are id , categorys and products fetched from database attention to the id
id is unique
i need to the sensitive url like zend framework url. for example :http://stackoverflow.com/questions/621380/seo-url-structure
how i can convert that url to the new url like this
is there any way?!!
You'll need to store a unique value in your database with a field name such as 'url' or something similar. Every time you generate a new product you will have to create this unique url and store it with the product information. A common way to do this is to take the name of the product and make it url friendly:
public function generateUrl($name)
{
$alias = str_replace(' ', '-', strtolower(trim($name)));
return preg_replace('/[^A-Za-z0-9-]/', '', $alias);
}
Calling this method:
$url = $this->generateUrl("My amazing product!");
echo $url;
will output:
my-amazing-product
You'll need to check that the output from this function does not already exist in the database as you will use this value to query on instead of the id.
If you apply this logic to the categories as well, you can have easily readable and descriptive urls like the one below. You may need to tweak your routing before this works correctly though.
http://quickstart.local/public/awesome-stuff/my-amazing-product
You could use ZF's Zend_Controller_Router_Route. For example, to make similar url to those used by SO, one could define a custom route in an application.ini as follows (assuming you have controller and action called questions and show respectively):
resources.router.routes.questions.route = '/questions/:id/:title'
resources.router.routes.questions.type = "Zend_Controller_Router_Route"
resources.router.routes.questions.defaults.module = default
resources.router.routes.questions.defaults.controller = questions
resources.router.routes.questions.defaults.action = show
resources.router.routes.questions.defaults.id =
resources.router.routes.questions.defaults.title =
resources.router.routes.questions.reqs.id = "\d+"
Having such a route, in your views you could generate an url as follows:
<?php echo $this->url(array('id'=>621380,'title' => 'seo url structure'),'questions');
// results in: /myapp/public/questions/621380/seo+url+structure
//OR if you really want to have dashes in your title:
<?php echo $this->url(array('id'=>621380,'title' => preg_replace('/\s+/','-','seo url structure'),'questions');
// results in: /myapp/public/questions/621380/seo-url-structure
Note that /myapp/public/ is in the url generated because I don't have virtual hosts setup on my localhost nor any modifications of .htaccess made. Also note that you don't need to have unique :title, because your real id is in :id variable.
As a side note, if you wanted to make it slightly more user friendly, it would be better to have your url as /question/621380/see-url-structure rather than /questions/621380/see-url-structure. This is because under this url you would have only one question, not many questions. This could be simply done by changing the route to the following resources.router.routes.questions.route = '/question/:id/:title'.
EDIT:
And what to do with categories and products that you have in your question? So, I would define a custom route, but this time using Zend_Controller_Router_Route_Regex:
resources.router.routes.questions.route = '/questions/(\d+)-(d+)/(\w*)'
resources.router.routes.questions.type = "Zend_Controller_Router_Route_Regex"
resources.router.routes.questions.defaults.module = default
resources.router.routes.questions.defaults.controller = questions
resources.router.routes.questions.defaults.action = show
resources.router.routes.questions.map.1 = category
resources.router.routes.questions.map.2 = product
resources.router.routes.questions.map.3 = title
resources.router.routes.questions.reverse = "questions/%d-%d/%s"
The url for this route would be then generated:
<?php echo $this->url(array('category' => 6213,'product' => 80,'title' => preg_replace('/\s+/', '-', 'seo url structure')),'questions' ); ?>
// results in: /myapp/public/questions/6213-80/seo-url-structure
Hope this will help or at least point you in the right direction.

How to create a custom router in Zend-Framework?

I am using a custom Router to enable pages like:
mytutorialsite.com/category/:categoryname
# added to application.ini
resources.router.routes.categorynameOnCategory.route = /category/:categoryname
resources.router.routes.categorynameOnCategory.defaults.module = default
resources.router.routes.categorynameOnCategory.defaults.controller = category
resources.router.routes.categorynameOnCategory.defaults.action = categoryname
I also have database table 'categories' in which all categories are stored. For example, let's say the following categories are currently stored in my database:
- html
- css
- js
- php
This means, the following pages exist:
mytutorialsite.com/category/html
mytutorialsite.com/category/css
mytutorialsite.com/category/js
mytutorialsite.com/category/php
But when you visit a page with a categoryname that is not listed in the database, like:
mytutorialsite.com/category/foo
You should get a 404 Page Does Not Exist message.
How do I achieve that?
I think you mean with categoryname as action in your route the :categoryname should be used as an action? There are two methods you can use. First is you add only the routes to the router where categories exist. When category/foo is requested the router won't recognize the route and throw the 404 error.
The second option is you catch all category/* routes and inside your action you check if the category exists.
For the first option, add a frontController plugin with a routeStartup function. In this hook you can do:
public function routeStartup(Zend_Controller_Request_Abstract $request)
{
// Get the router
$router = Zend_Controller_Front::getInstance()->getRouter();
// Fetch all your categories
$category = new Application_Model_Category;
$categories = $category->fetchAll();
// Loop and add all individual categories as routes
foreach ($categories as $category) {
$route = 'category/:' . $category->name;
$params = array(
'module' => 'default',
'controller' => 'category',
'action' => $category->name
);
$route = new Zend_Controller_Router_Route($route, $params);
$router->addRoute($category->name, $route);
}
}
The other method is more simple for the route. In your application.ini:
resources.router.routes.category.route = "category/:action"
resources.router.routes.category.module = "default"
resources.router.routes.category.controller = "category"
Now all requests from category/SOMETHING will go to the default module, category controller. The dispatcher checks if the action SOMETHING exists. When it does, it executes the action. When not, a Zend_Controller_Action_Exception ("action does not exist") is throw.
So in short, both methods work. With the first you get more control. The second is more simple but when e.g. an editAction, addAction or removeAction in the categoryController exist, they can be triggered as well (so be careful with that method).
PS. Of course, the routeStartup function should have a caching mechanism to prevent a database query on each request.

Categories