How to hide data from URL string on Codeigniter - php

I am beginner on Codeigniter so I need help.
I am making blog site with Codeigniterand I don't know how to hide controller class, and ID from URL.
Like I have generated following URL: http://localhost/codeigniter/index.php/content/some-title/123.
content is my controller class name.
some-title is my article title.
123 is my article id.
I want to keep only title to show.
For example: http://localhost/codeigniter/index.php/some-title.
Any help would be appreciated!!!

You has add your link config/routes.php
http://localhost/codeigniter/index.php/content/some-title/123
Ex: $route ['some-title/(: any)'] = 'content/some-title/$123';

You can do this:
$route['(:any)'] = 'content/view/$1';
and in your content controller you must have a method like the one follow:
public funtion view($pagetitle){…}

In the routes.php make sure you are using this line after your other routes which starts with some text.
$route['(:any)'] = 'content/show_article_post/$1';
Also make sure your slug for each article is unique in your database. There is no other way to send hidden id from get request.

Related

Display dynamic URLs in CodeIgniter for seo

I'm building an online store based on CodeIgniter. I'd like URLs to look like this? What is the solution for this type of SEO friendly url.
http://example.com/[product-category]/[product-sub category]
I need this url:
example.com/women/sarees-sari
But my url is generated
example.com/Product/item/MQ==/women/sarees-sari
/Product/ is my controller,
/item/ is function name,
/MQ==/ is my product id
You can use routing to handle your request url. It's simple. For example for your case:
$route['women/sarees-sari'] = 'Product/item/MQ==';
Codeigniter has _remap function that can be called on controllers. So you can call this on core controller or main controller, and call your function that wish.
CodeIgniter has very good routing System so you can modify your url as per your requirement and linking using /application/config/routes.php file.
If you open this file first time, you will see only default controller, i.e $route['default_controller'] = 'welcome';
but you can add as many routes as you want. Like in your case for seo, you should add
$route['women-sarees-sari'] = 'Product/item/MQ=='; and this will route the user from www.example.com/women-sarees-sari to correct controller and method.

remove controller and function name from url codeigniter

i am currently coding in codeigniter and i want to remove the controller and function name from url so that only product slug is displayed in url.
currently my url is like
www.example.com/main/singlePage/41/product-slug
I want the url to be like
www.example.com/product-slug
To do this create a "catch all" route $route['{any}'] = 'controller/function'
and in this function of the controller get the slug $slug = $this->uri->uri_string() and decide what to do based on it.
We can use routes for that, In your application/config/routes.php.
See the example,
$route['{any}'] = 'pages/slug_on_the_fly'
Ref: https://codeigniter.com/user_guide/general/routing.html

PHP codeigniter - multiple parameters in routes?

I've this in my config/routes.php:
$route['music/artist/(:any)'] = "music/artist/index/$1";
I've an artist controller inside controller/music/ and index method.
So if I go to browser with domain.com/music/artist/michael then with the following line in controller I can get the data michael.
$this->uri->segment(3);
Please correct me if I'm wrong but here's the real question.
Now I want to have this on link.
domain.com/music/artist/michael/video/beat-it/
I want to have a video function inside the artist controller and inside I want to get the dynamic data, michael and beat-it.
So the routes config I've these:
$route['music/artist/(:any)'] = "music/artist/index/$1";
$route['music/artist/(:any)/video/(:any)'] = "music/artist/video/$1/$2";
If I go to the video link it seems it just hit the index function.
How can I make the video link work?
Routes will run in the order they are defined. Higher routes will always take precedence over lower ones.
Switch the places of routes. Most specific routes need to be parsed first because (:any) placeholder would take anything there.
$route['music/artist/(:any)/video/(:any)'] = "music/artist/video/$1/$2";
$route['music/artist/(:any)'] = "music/artist/index/$1";
Reference.

PHP codeigniter, how do I make different key in the url so it will call to different controller name instead of the actual controller name?

lets say my url is:
http://www.mysite.com/controller1/
I want to make routing so it wont look for the controller1, it will search for controller1 as the key.
so I will have a key like array for the controllers
<?php
$controllers_mapArr = array(
'controller1'=>'actual_controler_name',
);
?>
it will execute the value of the controller key 'actual_controler_name' but the URL will show controller1
any ideas?
You can easily do this using codeigniter's routing functionality, you have to add rules in application/config/routes.php file, following are the examples for doing
$route['journals'] = "blogs";
if your url is like http://example.com/journals then your URL will call blogs controller
$route['journals/joe'] = "blogs/users/34";
using this rule your if you access your url http://example.com/journals/joe then it will call blogs controller's users method with 34 as a value for function argument.
check the detailed document or routing here : http://ellislab.com/codeigniter/user-guide/general/routing.html
Ok found the answer with a littel help from my friend actually is one of the basics in codeigniter
any way there is the answer:
http://ellislab.com/codeigniter/user-guide/general/routing.html

Working with CodeIgniter routes?

I think this is a route issue but I'm not sure. I have a page with this URL:
siteurl.com/kowmanger/titles/titles/edit/$id
I'm trying to find out that when I'm on this page I load the titles page it says page not found so I need to tell it that the $id is just a paramter so I can use it to get the data of the title.
UPDATE :
So I decided to change my titles controller so that there's a edit and add function inside of the titles controller that way they dont' have separate controllers when they are in fact methods.
So now I have:
kansasoutalwwrestling.com/kowmanager/titles/titles - list of titles
kansasoutalwwrestling.com/kowmanager/titles/titles/add - addnew form
kansasoutalwwrestling.com/kowmanager/titles/titles/edit/$id - edit form
I don't have any routes set up so far for this. For some reason though I"m getting the same page for both of these page.
kansasoutalwwrestling.com/kowmanager/titles/titles/add - addnew form
(right link url) kansasoutalwwrestling.com/kowmanager/titles/add -
addnew form
I need a route so that it'll show the correct url if the add method is accessed.
Also I need to set up a route so that if the correct edit link is accessed it sees the id attached to the end of the url and it'll accept it so that I can do a my database query to get the title data.
UPDATE: So to reiterate I have a module(subfolder) called titles. Inside of the module I have a controller called titles and inside of that controller I have 3 functions called index(), add(), edit().
I tried using Chris's suggestion on the routes but its not routing correctly. Also wanted to mention I'm using wiredesignz modular separation framework if that matters.
Any additional ideas?
Possible answer based on your post, not one hundred percent your entire structure but if i had to guess based off the post I would try this as my routes first..
$route['titles/titles/edit/(:any)'] = 'titles/titles/edit/$1';
$route['titles/titles/add'] = 'titles/titles/add';
$route['titles/titles'] = 'titles/titles';
$route['titles'] = 'titles/index';
Are you using custom routing in your configuration files ?
The general routing protocol used by codeigniter is like this:
domain.com/controller/methode/param1/param2/param3
This being said, your url
siteurl.com/kowmanger/titles/titles/edit/$id
corresponds to something like this :
class Kownmanger extends CI_Controller
{
public function titles($titles, $action, $id)
{
}
}
In case you are using sub-folders in your controllers folder, what I have just said will change, Could you please tell us what's your directory structure ?

Categories