Removing white space of urls with - in cakephp routing?
I have a URL like http://abc.com/users/index/University of Kansas
and i want to make i开发者_如何转开发t University-of-Kansas
. How is it possible via mysql using Cakephp ???
Use can use the Cake built in Inflector::slug($data, '-');
Source: http://api.cakephp.org/class/inflector#method-Inflectorslug
So you would get the string "University of Kansas" from the $this->params['url']
:
$data = $this->params['url'][....]:
$slug = Inflector::slug($data, '-');
I'm not sure how your data is being populated, but you probably want to store a tag, or slug field along with the full title. So your database would have both "University of Kansas" and also "University-of-Kansas" in a separate field. When you save an entry, you can auto-generate the latter field w/ a regex such as:
$slug = preg_replace("/[^-_0-9A-Za-z]/", "-", $title);
Depending on how your CakePHP is set up, you'd probably want to create a route that passed this slug value into the controller, so you could then look up the right entry in the database using that field.
http://cake-syrup.sourceforge.net/ingredients/sluggable-behavior/
This is a behavior that allows your model to create slugs when records get saved or edited.
精彩评论