How to create custom urls in cakephp - php

I have created a cakephp app. I have urls looks like
www.mysite.com/products/search/hardware
It loads fine. But I want urls that looks like
www.mysite.com/hardware
Can this is achieved by setting route connect
I appreciate any help.
Thanks.

Yes, you can use a route.
Router::connect(
'/hardware',
array('controller' => 'products', 'action' => 'search', 'hardware')
);
For a more general solution (any category name routing to products/search) see http://book.cakephp.org/view/948/Defining-Routes

You could add something like
Router::connect('/hardware',
array('controller' => 'products', 'action' => 'search'),
array('pass' => array('search'), 'search' => 'hardware'));
in your routes.php file, but then you would have to do it for each searchable item.
The problem you will be facing if you want something automatic, is that you need a way to differentiate your searchable products from every other model you have. So maybe you should settle for another type of URL like
www.mysite.com/products/hardware
or
www.mysite.com/s/hardware
and use the appropriate routes accordingly.

How about creating hardware_controller in your controllers folder.

Related

Is it possible to dynamically change view name or create a view that does not exist yet in phalcon?

I would like to know how can i do this in phalcon. I have a web site build with phalcon. All is working great now i stumbled upon a problem, here is what i need.
When a user clicks on a post that was created by another user. It takes him to this post with pictures and all things he entered to DB. I would like that in browser the name of this view is not like www.website.com/posts/index but that it is like www.website.com/posts/Nameofthepost, and like that for each other postings on the website. So that all posts (really ads) show their name up in browser. I hope i wrote everything understandable.
Appreciate all suggestions
That has to do with routing doesn't it? I modified this from my own code, I used grouping, you don't have to. I didn't test this code.
// routes.php
$router = new \Phalcon\Mvc\Router();
$router->setDefaultModule("__YOUR_MODULE__");
$router->removeExtraSlashes(true);
... your other routes ...
// posts group
$posts = new \Phalcon\Mvc\Router\Group(array(
'module' => '__YOUR_MODULE__',
'controller' => 'posts',
'action' => 'index'
));
// All the routes start with /post
$posts->setPrefix('/post');
$posts->add('/{postName}/:params', array(
'action' => 'index',
'params' => 2
));
// Maybe this will be enough for your needs,
// the one above has a catch all params, which
// has to be manually parsed
$posts->add('/{postName}', array(
'action' => 'index',
));
$posts->add('[/]*', array(
'action' => 'index',
));
$router->mount($posts);
unset($posts);
... other routes ...
return $router;
On your controller, you can get the postName param this way:
$this->dispatcher->getParam('permaPath');
As shown in the phalcon routing documentation, you can use regex in your routing config, something like this?
$posts->add('/{postName:[-0-6_A-Za-z]+}/:params', array(
'action' => 'index',
'params' => 2
));
So, only -_, 0-9, A-Z, a-z allowed for postName. If the URL had a comma in there or something, then route doesn't match, 404 page not found.

Cakephp route all model to different name

I'm not quite sure how to do this, I'm not familiar enough with cakephp routing.
Basicly, I have model like: Contact and Project. However, after redesign and redevelop and using the same stuff but different term..
I need to change something like:
myapplication.com/contacts/add --> myapplication.com/customers/add
myapplication.com/contacts/edit --> myapplication.com/customers/edit
myapplication.com/projects/add --> myapplication.com/tasks/add
myapplication.com/projects/edit --> myapplication.com/tasks/edit
or any other view.
How should I do this?
I know standard way:
Router::connect('/dashboard', array('controller' => 'projects', 'action' => 'dashboard'));
Router::redirect('/projects/dashboard', '/dashboard');
I believe I can do it one by one, but that's not very intuitive.
Try this.
Router::connect('/customers/:action', array('controller' => 'contacts'));
Router::connect('/tasks/:action', array('controller' => 'projects'));
It might be also a good idea to read this section about routing. It is explained there in detail how to do it.

Cakephp : What is wrong with my pagiantion and routing?

I am using cakephp 1.3.X . I am experiencing some problem with url routing and pagination
My URL will look like
http://project.dev/search/hamburg/place:1
Below is my Routing code
Router::connect('/search/:slug/*', array('controller' => 'searches', 'action' => 'index'));
Below is controller where I am coming to the search page
$this->redirect(array('controller'=>'searches','action'=>'index','slug'=>$slugUrl,$query));
My problem is when I go to the next page ,that won't showing the place:1 , just linking to search/hamburg/page:2 ie. place is missing
My index.ctp paginator options are given below
$paginator->options = array(
'url'=>array(
'controller'=>'searches',
'action'=>'index',
'slug'=>$this->params['slug'],
));
What going wrong with me. I've had searched a lot in cake articles , but nothing works for me. Please advise me
Add.
Router::connectNamed(array('slug'));
At the top of your router.php
Or define third pass param in your route, to pass slug.
Router::connect('/search/:slug/*', array('controller' => 'searches', 'action' => 'index'), array('pass'=>array('slug')));
Update view
$paginator->options = array('url' =>
array_merge(array('slug' => $this->params['slug']), $this->passedArgs)
);

Remote controller name from CakePHP URL

I have a problem with the url rewriting.
The problem that I am facing is that currently our urls are like this:
http://www.xyz.com/sc_users/index
I dont want the controller name to be shown in that url.
Is there a way to achieve that??
First of all thank your guys..
Like I have 8 controllers I dont want the controller name to be shown in my url....this is what I want..
To be more precise no controller name in my url
You can define custom routes in app/config/routes.php. You'll find the all about routes in the CakePHP cookbook under Defining Routes. For example, a custom route can look like this:
Router::connect(
'/the_url_you_want_to_use/*', array('controller' => 'sc_users', 'action' => 'index')
);
You need to read up about CakePHP routing, look at the examples under 'defining routes'. Update your question with what you would actually like your URLs to look like and we will be able to help you more effectively.
That's simple :
there is a file called routes.php in /config directory :
You can do url rewriting stuff there like this :
Router::connect('/pages/*', array('controller' => 'cmsPage', 'action' => 'render'));
You can pass more complicated variables to your controller :
Router::connect('/:id-:lang-:profile-:firstName-:lastName-:profile.htm',
array('controller' => 'profiles','action' => 'view'),
array('id'=>'[0-9]*', 'lang'=>'fr','firstName'=>'[^-]*','lastNAme'=>'[^-]*','profile' => $util->keywords['profiles'][0]['fr'], 'pass' => array('id', 'lang'),'profile' => $util->keywords2['profiles'][0]['en'])
)
;
As you can see in the last example I have passed 2 parameters to the controller through 'pass' => array('id', 'lang')

Zend_Router omitting param-key

I've got a question considering Zend_Controller_Router. I'm using a a modular-structure in my application. The application is built upon Zend-Framework. The normal Routes are like this:
/modulename/actionname/
Since I always use an IndexController within my modules, it's not necessary to provide it in the url. Now I am able to append params like this:
/modulename/actionname/paramkey/paramvalue/paramkey/paramvalue
So this is normal in ZF, I guess. But in some cases I don't want to provide a paramkey within the url. For example I want a blog-title to be shown within the url. Of course this is intended for SEO:
/blog/show/id/6/this-is-the-blog-title
In this case, blog is the module, show is the action. id is a paramkey and 6 is the id of the blogpost I want to show. this-is-the-blog-title is of course the headline of the blogpost with the id 6. The problem is, that if I do use the assemble()-method of the router like this:
assemble(array('module' =>'blog',
'action' => 'show',
'id' => $row['blog_id'],
$row['blog_headline_de'] . '.html'));
the url results in:
blog/show/id/6/0/this-is-the-blog-title.html
As you can see a 0 is inserted as a key. But I want this 0 to be omitted. I tried this by using the blogtitle as key, like this:
assemble(array('module' =>'blog',
'action' => 'show',
'id' => $row['blog_id'],
$row['blog_headline_de'] . '.html' => ''));
This results in:
blog/show/id/6/this-is-the-blog-title.html/
Now the 0 is omitted, but I've got the slash at the end.
Do you have any solution to get an url without 0 as key and without an ending slash?
Regards,
Alex
You might want to use a custom route for this:
$router->addRoute(
'blogentry',
new Zend_Controller_Router_Route('blog/show/:id/:title',
array('controller' => 'index', 'module' => 'blog'
'action' => 'info'))
);
And call your assemble with the route as second parameter. See the Zend_Controller_Router_Route section of the documentation for more details (they even provide examples with assemble).
Or in a more general way:
$router->addRoute(
'generalseo',
new Zend_Controller_Router_Route(':module/:action/:id/:title',
array('controller' => 'index'))
);

Categories