I was used to putting id's in the URL to map to an item in the database:
/hotels/1
but what if I want to get the name of the hotel with an id of 1 and put it in the URL and replace spaces with hyphens?
/hotels/hotel-bianca
I am using Kohana and there is the concept of routing (which is pretty much present in all MVC frameworks), but I can't seem to get it working
How do I go about doing this?
Since I know nothing about kohana, I am going to present a possible PHP answer.
Could you pass the id through the URL and request it with PHP, and if you're passing the name of the hotel, have that correspond to the item in the database with the hotel-name as its field?
For this purpose I use special field in db table named url :)
So for example /controller/open/urladress will look for url field with 'urladress' inside to open :D
I don't think you can change uri on fly)
In the controller you could search the database for the name. I am used to Kohana 2.3.4 with ORM so this is how I would do it:
// first you need to replace all hyphens with spaces
$name = str_replace('-', ' ', $parameter);
// search your db for the hotel by name
$hotel = ORM::factory('hotel')->where('name', $name)->find();
// check to make sure it is loaded
if ( ! $hotel->loaded)
{
// Do something i.e. 404 page or let them know it wasn't found
}
This would let you specify by name. You could also add a check to see if the parameter was integer or not and search by id if it was.
Related
I'm developing a web application and want the user to define the name of the URL using an input field.
I know how to find the name of the parent folder but don't know how to overwrite it. I also know how to get the value of the input field so for the sake of keeping it as simple as possible, a hardcoded value would be fine as a solution.
This is how I get the name of the parent folder:
$moduleName = basename(dirname(__FILE__));
If I echo $moduleName, I get the name of the parent folder, which determines the name of the url.
I want to do something like:
basename(dirname(__FILE__)) = 'new-name';
But I don't know if it is possible to do with php? :)
Try function.rename
rename(realpath(dirname(__FILE__)).'/myoldfolder',realpath(dirname(__FILE__)).'/mynewfolder');
Credits:
Coderwall
I have a problem in url structure ...
I have 3 categories
streaming
movies
serials (dramas)
My Url structure is
example.com/stream/CategoryName/Channel Name
example.com/movie/CategoryName/MovieName
example.com/serial/CategoryName/Serial Name
I've added in this code in routes.php
$route['stream/(:any)/(:any)'] = "main/stream/$1/$2";
$route['movie/(:any)/(:any)'] = "main/movie/$1/$2";
$route['drama/(:any)/(:any)'] = "main/drama/$1/$2";
with this structure the website is working good. but client want that the url only contain the category name / Video Name
If i remove stream/movie/drama before /(:any)/(:any) in route then the controller get confused which function to call.
Example what i need
example.com/CategoryName/Channel Name
example.com/CategoryName/MovieName
example.com/CategoryName/Serial Name
What should i do ?
Uh this is really bad situation.
My solution would look something like this.
$route['steam-(:any)/(:any)'] = "main/stream/$1/$2";
$route['movie-(:any)/(:any)'] = "main/movie/$1/$2";
$route['drama-(:any)/(:any)'] = "main/drama/$1/$2";
But then u must edit your controller methods to explode first uri by '-' or something like that u can use parsed uri as before in method.
Hope this will help you.
I am using laravel 4 routing for create friendly url for my project. I want to get url with this format:
http://www.mywebsite.com/post/post-slug-content-123 . post-slug-content is my slug, and 123 is my post id.
my route:
Route::get('post/{name}-{id}','postController#detail');
It not work because laravel detect "post" is my {name} variable, and "slug" is my {id} variable, but i want "post-slug-content" is {name} and 123 is {id}. Any idea ???
using - wont work and which - will it consider breaking in post-slug-content-123
if your url is http://www.mywebsite.com/post/post_slug_content-123
or http://www.mywebsite.com/post/postSlugContent-123 try using
Route::get('post/{name}-{id}','postController#detail');
because you have multiple - and it will consider it as a separate URL
Or you may try
1: Route::get('post/{nameId}','postController#detail'); where both your name and id is present, further you could break them in your postController.
2: Route::get('post/{name}/{id}','postController#detail'); this will work fine
or use stackoverflow kind id followed by name
3: Route::get('post/{id}/{name}','postController#detail');
you can also use http://www.mywebsite.com/post/post-slug-content_123 as said by #itachi
4: Route::get('post/{name}_{id}','postController#detail');
Further reference for passing parameters Here.
I have spent hours searching to find where magento stores full county name.
We can get a full list of countries using this code:
$_countries = Mage::getResourceModel('directory/country_collection')
->loadData()
->toOptionArray(false);
This will return an array with country code and name, I investigated the directory module, and found out this call gets data from the table
directory_county
But this table don't have full county name! So where is it stored?
and how is it retrieved using that call?
Thanks in advance.
Ok so to compensate for my wrong answer. Here is how this works:
/lib/Zend/Locale/Data/en.xml - if your store is in english, else another xml in the same directoery is read. Every country is there and its code under the xml tag <territory>
The xml is cached using the Zend_Cache_Core class.
/lib/Zend/Locale.php - function getTranslation invokes the lib/Zend/Cache/Core.php class
to load from the cache.
Example: If you change the name of some country in en.xml and clear the magento cache. You will see the change when u invoke your code again.
Full country names are not stored in database. Magento uses inbuilt Zend functionality.
Check file: lib/Zend/Locale/Data/Translation.php for full list.
Use the Zend_Local translation.
<?php
$code = 'EN';
echo Mage::app()->getLocale()->getTranslation($code, 'Territory', null, 2);
?>
Use the column 'iso2_code' from the table 'directory_country' for your $code.
Magneto only stores country codes in DB, and relies for names on Zend's Locale module to provide translated names, for different locale.
By the toOptionArray method it invokes the Zend_Locale class to get the translated value.
Refer $name = Mage::app()->getLocale()->getCountryTranslation($data['value']);, which gets to Mage_Core_Model_Locale and then to Zend_Locale.
It decides which of the node from the data to read, by the switch case statement in Zend_Locale_Data::getContent() line# 962, 963
Magento caches the names, so if you make any change to XML files, make sure to clean your cache folder to get what you seek.
I have some questions concering routing with Codeigniter. What I´m doing now is the following:
$route['articles/(:num)'] = 'articles/view/$1'; // $1 will contain an ID
This means that example.com/articles/123 will work perfectly and load an article with an ID of 123. But I also want to have the possiblilty to add the aticle´s title to the URL (for SEO). Example: example.com/articles/123/article-title
What I want is pretty much the same thing as Stack Overflow: stackoverflow.com/questions/123/the-title
How can I do that?
I´m also wondering how Stack Overflow works. If I go to stackoverflow/questions/111 the title will automatically be added to the url. Is that done with php redirect()?
I have done something similar in the past; I can't find it know but IIRC (it was months ago) You can use a route like you did, and also add a more specific one, like
$route['articles/(:num)/(:any)'] = 'articles/view/$1/$2';
$route['articles/(:num)'] = 'articles/view/$1';
As you can see, both map to the same method, which is kind of "overloaded"; you can make up for a missing parameter by using a default value:
function articles($id,$slug = FALSE)
{ }
and simply ignore the second parameter in your article retrieval.
As for adding the title you can:
have a "slug" field in your database, created when the article is saved. You can use the comfortable url_title($title,'dash',TRUE) function (in the url helper), which takes the $title, uses the dash as separator, and make it all lowercase;
use the above function and convert the title of the article (after you retrieved it from the database) "on-the-fly"; just check on the article() method if the 2nd parameter isn't false and you'll know if you need to create the slug or not;
As for how to show the slug even when using an url without it you can make, as you guessed, a redirect, but since both routes point to the same method it won't change anything for you.
Oh, uhm, beware of loops while calling the redirect, check carefully ;)
I suggest you to create a slug field into your table and complete it with the url you want to use as id.
Let me explain.
You have this table
id
title
slug
when you save an article into your db you can dinamically create a slug, for example:
id: 1
title: My first post
slug: 1-my-first-post
then you can use the slug (in this case 1-my-first-post) ad id for the page, you can call it:
www.example.com/articles/1-my-first-post
obviusly you need to handle it in your db slect
As we discussed on the comments.
You can create a route several times and with different parameters each, like:
$route['articles/(:num)/(:any)']
$route['articles/(:num)']
I would create a function with a redirect, adding or not the title to it.
Hope it helps.