I have an e-commerce site, created on Codeigniter. It has products and it's descriptions. The URL for description is : http://localhost/online_ups/products/product-details/26. Now, I want this URL id (26) to be encrypted in the URL and still work the same as it is working now.
I have no idea of implementing this functionality. Can someone help regarding the same?
The controller name is Product and the Model name is Productsdisplay.
You can do it like this.
URL for description is : http://localhost/online_ups/products/product-details/26.
For that url you can do like below.
$product_id = urlencode(base64_encode(26));
$url = 'http://localhost/online_ups/products/product-details/'.$product_id;
And then in controller you can retrieve the id by below way.
function product-details ($product_id) {
$product_id = base64_decode(urldecode($product_id));
// you can get actual product id
}
Related
I am trying to rewrite some of the pages URL in my web app, for example, I have a company details page and I want the URL of this page to contain the name of the company without space or special characters and to have the id too, and I have no idea on how to do that or where to start.
I tried adding the name of the company in the route as a parameter like this:
Route::get('/{RS}-{id}', 'App\Http\Controllers\SiteController#getDetails')->name('details');
It didn't work for some reason and even if it worked there's no way to replace the special characters with their normal form and replace the space with dashes.
This the URL I'm getting: http://mywebsite.come/entreprise_details-13109
this the URL I want: http://mywebsite.come/yoorika-managements-13109 ( with Yoorika Managements being the name of the company )
I searched a lot but I can't seem to find what I am looking for, I just need someone to help me find the right term. thank you!
In route :
Route::get('/{input}', 'App\Http\Controllers\SiteController#getDetails')->name('details');
In controller :
public function getDetails($input)
{
$input_array = explode("-", $input);
$name = $input_array[0];
$id = $input_array[1];
$data = MyModel::where('id', $id)->get();
// return data to view
}
In list view, you may display data like this (table cell ) :
<td> <a href="/{{name .'-'.{{id}}"> {{name .'-'.{{id}}<td/>
How to make my codigniter url seo friendly.
this is my url
http://localhost/picker2/web/search?category=doctor
i want url like this
http://localhost/picker2/web/search/doctor/
read this article www.askaboutphp.com/58/codeigniter-mixing-segment-based-url-with-querystrings.html
to create url like this : http://heloo.com/article/codeigniter-based-url
add this code in routes.php
$route['article/(:any)'] = "article/readmore/$1";
description :
article : class name
readmore : method from class article
$1 : get value from uri segment 2 value
Actually you have two options
1) Using Routes (this one already denyptw discussed right ?)
2) use the URL Helper url_title function
Note : You can use Parameter instead of query string
http://localhost/picker2/web/search?category=doctor
http://localhost/picker2/web/search/doctor/
web controller , search function , doctor parameter
example :
class Web extends CI_Controller
{
public function search($value)
{
//use this $value in your searching logic
}
}
You need Example of URL Helper
check this link Codeigniter URL: How to display id and article title in the URL
you can do this using routes.(Elislab)
as a simple trick.(Using Controllers)
each and every page create a controller. (ex: if contact create contact controller, if product create product controller.) So each and every link will be look like(if you click product) www.example.com/product,(if you click contact) www.example.com/contact.
If you want something to do with your product then write that method inside the product controller.(ex if you create cart then URL show www.example.com/product/cart )
I have defined a route in laravel 4 that looks like so :
Route::get('/books/{id}', 'HomeController#showBook');
in the url It shows /books/1 for example , now i'm asking is there a way to show the name of the book instead but to keep also the id as a parameter in the route for SEO purposes
thanks in advance
You could also do something like this:
Route::get('books/{name}', function($name){
$url = explode("-", $name);
$id = $url[0];
return "Book #$id";
});
So you can get book by id if you pass an url like: http://website.url/books/1-book-name
if your using laravel 8, this may be helpfull.
In your Controller add this
public function show(Blog $blog)
{
return view('dashboard.Blog.show',compact('blog'));
}
In your web.php add this
Route::get('blog/{blog}', [\App\Http\Controllers\BlogController::class,'show'])->name('show');
Then add this to your model (am using Blog as my Model)
public function getRouteKeyName()
{
return 'title'; // db column name you would like to appear in the url.
}
Note: Please let your column name be unique(good practice).
Result: http://127.0.0.1:8000/blog/HelloWorld .....url for a single blog
So no more http://127.0.0.1:8000/blog/1
You are welcome.
You can add as many parameters to the url as you like, like this:
Route::get('/books/{id}/{name}', 'HomeController#showBook');
Now when you want to create an url to this page you can do the following:
URL::action('HomeController#showBook', ['id' => 1, 'name' => 'My awesome book']);
Update:
If you are certain that there will never be two books with the same title, you can just use the name of the book in the url. You just need to do this:
Route::get('/books/{name}', 'HomeControllers#showBook');
In your showBook function you need to get the book from the database using the name instead of the id. I do strongly encourage to use both the id and the name though because otherwise you can get in trouble because I don't think the book name will always be unique.
You can also use model binding check more on laravel docs
For example
Route::get('book/{book:name}',[BookController::class,'getBook'])->name('book');
The name attribute in "book/{book:name}" should be unique.
My question is how to append country name in codeigniter url?
Lets say my website is http://mywebsite.com and I am opening the website from Canada so my url should be http://mywebsite.com/canada.
Meaning I just want to append the country name in the url, nothing change except this. I have read about routes of codeigniter, I have googled it but all in vain.
If anyone have a clue how to do this please let me know.
I guess you'd do that with a IP lookup to guess the country based on the request IP address ($_SERVER['REMOTE_ADDR']). Mapping the IP address to a location is pretty well documented. Once you have the country, redirect to the correct controller method.
That what you're looking for?
This is like for language code in url.
With my solution you can put any segment in your uri and hide it to CI.
http://site.com/page.html will be equal to http://site.com/canada/page.html and vice versa.
set_country_uri.php
//App location
$ci_directory = '/ci/';
//countries
$countries = array('canada','france');
//default country
$country = 'canada';
foreach( $countries as $c )
{
if(strpos($_SERVER['REQUEST_URI'], $ci_directory.$c)===0)
{
//Store country founded
$country = $c;
//Delete country from URI, codeigniter will don't know is exists !
$_SERVER['REQUEST_URI'] = substr_replace($_SERVER['REQUEST_URI'], '', strpos($_SERVER['REQUEST_URI'], '/'.$c)+1, strlen($c)+1);
break;
}
}
//Add the country in URI, for site_url() function
$assign_to_config['index_page'] = $country."/";
//store country founded, to be able access it in your app
define('COUNTRY', $country);
index.php
<?php
require('set_country_uri.php');
//ci code ...
So in your CI code use COUNTRY constant to know wich is used. And make your code like routing without take in consideration the country in the uri.
I'm having an issue with routing in codeigniter.
Lets say I have a controller named Pages, with a method named product that does the following:
public function product() {
$this->load->model('pages_model');
$productid = $this->uri->segment(3);
$data['product'] = $this->pages_model->getProduct($productid);
// ...load view, etc.
}
To access a particular product, my url will be www.example.com/pages/product/ID.
I want to setup a custom route so I can access the product by going to www.example.com/name-of-product.
However, putting
$route['name-of-product'] = 'pages/product/ID';
does not work. It will load the product view, but the product data will not be loaded. If I say
$route['name-of-product/:any/ID'] = 'pages/product/ID';
it works as it should, but I would rather not have the two additional segments at the end of the url.
You don't need 2 additional segments. One should be sufficient.
$route['PRODUCT_NAME/PRODUCT_ID'] = 'pages/product/PRODUCT_ID';
However, if I were you I would make the URL to have the first segment to be the id of the product instead.
$route['PRODUCT_ID/PRODUCT_NAME'] = 'pages/product/PRODUCT_ID';
That way, if I only know the product id, I wouldn't have to type example.com//123 which might cause some problem. If I'm not mistaken, if you do that, CI will try to load a controller named 123.