I have the following situation, I have some posts on my website which are access by id, lets say for example
http://websiteurl.com/posts/see/1
the posts controller, has a see method which looks for the id on the database, lets say it find it and the posts title is "Most amazing photos" , how could i configure the Route.php class of codeigniter so that this posts its accessible from an url similar to:
http://websiteurl.com/posts/see/most-amazing-photos-1.html
this posts are created dynamically and they are a lot, so setting one by one manually is not possible.
Thanks!
What you can do is use regex. Assuming that you will be creating different url tag for each photo you can have something like this in you route.php
$route['posts/see/(:any)'] = "posts/see/$1";
However it is necessary that you have unique url-tag for each photo otherwise it will be ambiguous to fetch photo from the url-tag.
Have it a go.
The url parameter you would like to achieve is called a slug
like wordpress does, you can query the POST via its id or its slug.
One way to implement this is every time a post is created, you save the slug to the db.the slug can be the title of the post or anything readable for SEO. So that everytime a user visits the site, your controller method may prcess the parameter if it is a slug or an id and continue the query using the given parameters.
then you can set up the routes like:
$route['posts/see/(:any)'] = "posts/see/$1";
Related
This is my URL :https://rentals.in/product/categories/41
Here 41 is the product id and its product name exist in DB is Electronics so i want to convert this URL as "https://rentals.in/product/categories/Electronics" dynamically using Routing function available in Codeigniter3.0. Is any callback function can use in routing for achieving above? or Any Other way?
CodeIgniter follow below routing by default to fetch data by their id, if you completely replace the id part and search by name then there would be chances of getting wrong data, it would give first matching name.
example.com/class/function/id/
I suggest you to create Url like this, and make last name part optional
rentals.in/product/categories/41/Electronics
Which will use id to get data and also display name of category
Just like SO does. Below both url opens same page.
//stackoverflow.com/questions/52051604
//stackoverflow.com/questions/52051604/how-to-replace-product-id-with-product-name-in-url-using-routing-in-codeigniter
Refer this answer to make optional param in CodeIgniter routing
I am working on a webapp. There are items accessible the way using item id in the url web.site/123, where 123 is item id. I want to redirect visitors to the full url page like web.site/123-the-item, and I also want to use full links within my website.
Any good idea how to do it?
I don't need the code, I am looking for a simple yet nice and effective idea. The title is obviously in the database, so there will be one select needed anyhow. But I am not sure, if I should redirect once and every single time visitor visits the site without checking if the url is full (doesn't seem nice) or should I check the url and redirect only if it doesn't match the title?
I am using nginx, php, mysql and laravel 5.2 in case there is a workaround for this scenario I am not aware of.
Try adding route like
Route::get('/{id}',['uses' => 'ContentController#byId'],'where'=>['id'=>'\d*']);
And in that action you can check in database if id exists and redirect user to proper url
You can check use regular expressions to check the if the incoming request has just the id and have them mapped to particular routes. In the digit route you can redirect.
E.g.
your route file should be something like this :
Route::get('item/{id}', function ($id) {
//redirect to the full name route
})
->where('id', '[0-9]+');
Route::get('item', 'SomeController#somemethod');
Read more here.
How I add pages for my website without create files for any page I want. To make understand let's say I need a details information page to read from mysql the rest of informaton of a specific ID BUT! in link i don't want to be with id
(site.com/details-product-idnumber)
i want to be only
(site.com/details-product-nameofproduct)
I know basicly this need to be rewrite from .htaccess but what I need to know about this function/system.
Another example is wordpress blog platform, when you add a post, in link it only the title of the ID post from mysql, what php code i need to use or what's the name of this function. Thanks.
may be you can use a unique_name (in your application a unique name for a product).. in your database table.. and just pass that unique_name in the link..
like..
site.com/details-product-unique_name
and your sql query should be like..
select * from product where unique_name = '$_GET["your_get_field"]';
let me know if you want any further guidance...
I've built a webpage that, in its products page has this prototype for its url
http://www.example.com/products/show/some-really-nice-product/512
The last bit of the url is the product id, which serves the sole purpose of searching the db for the product.
But, is it possible, using routing, to be able to hide that part and still be possible to search the product by its id?
Like
http://www.example.com/products/show/some-really-nice-product/ ??
Thank you
If you don't want to put a number in your URL, you can't later search by a number/id :)
So you could add a VARCHAR field to the table, say "urlized", and use "some-really-nice-product" as its content for the given ID (you'll need to 'urlize' all your product names and make them unique). Don't forget to index your new field...
Then, you could access the product page using this URL which is very SEO friendly (ends with ".html"):
http://www.example.com/products/show/some-really-nice-product.html
...by adding a route to CodeIgniter, somehow like this:
$route['^products/show/(.*).html'] = 'products/show/$1';
In your controller/model, search the database for the string that is passed to "show" method, instead of the ID
if you hide id you can't retrieve that from the url anymore
if you want to hide that anyway just do:
$route['products/show/(:any)'] = "products/show/$1/$2";
My site has articles and I would like the URL to be like so:
example.com/(article id)/(article title)
There are two reason I would like the URLs like so, SEO, and readability.
This setup is close to how stackoverflow is set up. And just like on stackoverflow, I don't want the title to be required but if it's not including in the I want the back-end to append the title to the URL.
To understand what I mean, take the current page's URL, remove the title form it and go to that new url. If you notice when the page is refreshed the title was automatically appended to the URL.
When I think about how to achieve this, it requires two queries. The first query will get the title based on the id, then form the new url, and redirect to it. The second URL will query the database with the id again this time displaying the article.
Is this the only way? Can I append the extra param without actually redirecting?
Side note: I am using cakePHP
I think that yes, this is the only way.
Call #1 to example.com/(article id): the server would query the database for the id, see that the url isn't the canonical one[1] and do a redirect.
Call #2 to example.com/(article id)/(article title) would behave as normal, meaning some more queries to the database...
^ I suggest you store an article's generated url path so you could use it in other places too.
This way, if no title was provided, the path would just be "/(article id)" and you wouldn't need to do a redirect here.