How to create username in codeIgniter? - codeigniter

I want address of my website user is mydomain.com/username/. But it seems very difficult to me using codeIgniter.Please help me what is best way to do it.
mysite.com/profile?user=username
is current url of profile but i want this
mysite.com/username
please help me and I'm not good english speaker so I'm sorry if you not understand my question.

In your routes.php file route every existing controller (you should have atleast one) to itself. For example if you have controller main in main.php file route it:
$route['main'] = "main";
$route['main/(:any)' = "main/$1";
The reason why you should route it twice is because you must make sure that opening http://yoursite/main works as well as http://yoursite/main/my_method/ etc. Do this for every other controller you have.
The next step is to route everything else to your users controller. For example you have a profile method that has 1 argument - the username.
$route['(:any)'] = "users/profile/$1";
So now you will have everything else routed to users/profile/username.
One thing to remember is that the topmost priority goes higher in the routes.php file so your routes file should look something like:
$route['main'] = "main";
$route['main/(:any)' = "main/$1";
$route['users'] = "main";
$route['users/(:any)' = "main/$1";
$route['(:any)'] = "users/profile/$1";
See if that works!

You can use the CI routing
In your route.php file, select all your users and create a route for each :
$aUsers = $this->oDb->getAllUsers();
foreach($aUsers as $oUser) {
$route[$oUser->username] = "profile/" . $oUser->username;
}
It should work.

Related

Codeigniter 3.1.16 routing issues

I'm having some troubles with routing in codeigniter.
My routing file is as below:
$route['admin/newgallery'] = 'gallery/do_upload';
$route['admin/listgallery'] = 'gallery/list';
$route['admin/create'] = 'posts/create';
$route['admin/listposts'] = 'posts/list';
$route['admin'] = 'admin/index';
$route['posts/(:any)'] = 'posts/view/$1';
$route['posts'] = 'posts/index';
$route['default_controller'] = 'pages/index';
$route['(:any)'] = 'pages/index/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
All routes work just fine except for the first two:
$route['admin/newgallery'] = 'gallery/do_upload';
$route['admin/listgallery'] = 'gallery/list';
When I type mypage/admin/listgallery it calls gallery/list correctly. The problem is when I type the address with the original controller/method (in this case gallery/list) it goes to the list page as well when it should call a 404 error! Every other routing rule I have set does that, except the first two!
Out of the Box, Codeigniter allows you to directly access any Controller/Method from the URL.
It also provides the creation of custom routes so you could have 10 or more urls all pointing at the same controller/method with parameter passing if that was your desire...
So in the case you ONLY want access to any Controller/Method that are defiend in the Routes config.
You need to test if the url is defined in the routes config array.
The main code is something like...
$this->load->helper('url');
if(!isset($this->router->routes[uri_string()])){
show_404(); // Or whatever you want ...
}
And you would put this in your controller's constructor you want to protect.
Of course you could create a common controller and extend those controllers you want to protect in this manner.
( NOT Recommended ) Or if you want to get really "hacky" you could put it in the system/core/controller constructor and make it system wide. SO Everything needs to be defined in a route.
NOTE: This breaks the 'default_controller'.

Code igniter url segment

currently my website url is
https://www.thefoolsbookie.com/main/inside?id=8
but I want this to be like this
https://www.thefoolsbookie.com/nfl
How can I do this?
Edit the application/config/routes.php file and add a new route for it
$route['nfl'] = 'main/inside';
It should be as simple as that :)
See https://ellislab.com/codeigniter/user-guide/general/routing.html for more examples.
Edit
I've worked with CI for a long time and I've never seen it use GET params in the routes file in that way. I'm not sure it is possible.
You could have it like $route['nfl'] = 'main/inside/8';
then in the main controller your inside method would look like:
public function inside($id)
{
//$id would contain the ID of 8 when you go to
//https://www.thefoolsbookie.com/main/inside/8
//or https://www.thefoolsbookie.com/nfl
}

How do I get a URL structure with useful information in CodeIgniter?

I've got a website which has a URL structure that is not at all useful for breadcrumbs, conducive to SEO, or intuitive for users. It's something like
asdf.com/directory/listing/{unique_id}/{unique-page-name}/
I would really like to change this to
asdf.com/{state}/{city}/{unique_id}/{unique-page-name}/
or something very similar. This way, I can implement breadcrumbs in the form of
Home > State > City > Company
Does anyone have any ideas as far as converting the current structure to one as I've described above? Any way I look at it, it seems that it'll require a complete overhaul of the website. It would just be great to be able to show users something like Home > Florida > Miami > Bob's Haircuts
Thanks!
You'd just need to be creative with your routes: http://ellislab.com/codeigniter/user-guide/general/routing.html
You can set up a route to catch all traffic and point it to directory/listing, then in your listing method - you can access the url segments manually. For example:
// application/config/routes.php
$route[':any'] = "directory/listing";
/**
you might have to play with this a bit,
I'm not sure, but you might need to do something like:
$route[':any'] = "directory/listing";
$route[':any/:any'] = "directory/listing";
$route[':any/:any/:any'] = "directory/listing";
$route[':any/:any/:any/:any'] = "directory/listing";
*/
// application/controllers/directory.php
function listing()
{
// docs: http://ellislab.com/codeigniter/user-guide/libraries/uri.html
$state = $this->uri->segment(1);
$city = $this->uri->segment(2);
$unique_id = $this->uri->segment(3);
$unique_page_name = $this->uri->segment(4);
// then use these as needed
}
OR, as is probably the case, you need to be able to call other controllers and methods -
You can change the URL to point to a controller, then do the listing stuff -
So your url would become:
asdf.com/directory/{state}/{city}/{unique_id}/{unique-page-name}/
and your route would become:
$route['directory/:any'] = "directory/listing";
Then, you'd need to update the uri segments in your listing method to match the 2nd, 3rd, 4th, and 5th segments.
This way, you could still call another controller and it wouldn't be caught by your custom route:
asdf.com/contact/ --> would still access the contact controller and index method
UPDATE
You could also get creative and use a regular expression to catch any urls with state names in the first uri segment - then push those to directory/listing and then all other controllers will still work and you don't have to add the directory controller in the url. Something like this might work:
// application/config/routes.php
$route['REGEX-OF-STATE-NAMES'] = "directory/listing";
$route['REGEX-OF-STATE-NAMES/:any'] = "directory/listing"; // if needed
$route['REGEX-OF-STATE-NAMES/:any/:any'] = "directory/listing"; // if needed
$route['REGEX-OF-STATE-NAMES/:any/:any/:any'] = "directory/listing"; // if needed
/**
REGEX-OF-STATE-NAMES -- here's one of state abbreviations:
http://regexlib.com/REDetails.aspx?regexp_id=471
*/

CodeIgniter - Dynamic URL segments

I was wondering if someone could help me out.
Im building a forum into my codeigniter application and im having a little trouble figuring out how i build the segments.
As per the CI userguide the uri is built as follows
www.application.com/CLASS/METHOD/ARGUMENTS
This is fine except i need to structure that part a bit different.
In my forum i have categories and posts, so to view a category the following url is used
www.application.com/forums
This is fine as its the class name, but i want to have the next segment dynamic, for instance if i have a category called 'mycategory' and a post by the name of 'this-is-my-first-post', then the structure SHOULD be
www.application.com/forums/mycategory/this-is-my-first-post
I cant seem to achieve that because as per the documentation the 'mycategory' needs to be a method, even if i was to do something like /forums/category/mycategory/this-is-my-first-post it still gets confusing.
If anyone has ever done something like this before, could they shed a little light on it for me please, im quite stuck on this.
Cheers,
Nothing is confusing in the document but you are a little bit confused. Let me give you some suggestions.
You create a view where you create hyperlinks to be clicked and in the hyperlink you provide this instruction
First Post
In the controller you can easily get this
$category = $this->uri->segment(3);
$post = $this->uri->segment(4);
And now you can proceed.
If you think your requirements are something else you can use a hack i have created a method for this which dynamically assign segments.
Go to system/core/uri.php and add this method
function assing_segment($n,$num)
{
$this->segments[$n] = $num;
return $this->segments[$n];
}
How to use
$this->uri->assign_segment(3,'mycategory');
$this->uri->assign_segment(4,'this-is-my-first-post');
And if you have error 'The uri you submitted has disallowed characters' then go to application/config/config.php and add - to this
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';
You could make a route that forwards to a lookup function.
For example in your routes.php add a line something like;
$route['product/(:any)/(:any)'] = "forums/cat_lookup/$1/$2";
This function would then do a database lookup to find the category.
...
public function cat_lookup($cat, $post) {
$catid = $this->forum_model->get_by_name($cat);
if ($catid == FALSE) {
redirect('/home');
}
$post_id = $this->post_model->get_by_name($post);
/* whatever else you want */
// then call the function you want or load the view
$this->load->view('show_post');
}
...
This method will keep the url looking as you want and handle any problems if the category does not exist.Don't forget you can store the category/posts in your database using underscores and use the uri_title() function to make them pretty,
Set in within config/routes.php
$route['song-album/(:any)/:num'] = 'Home/song_album/$id';
fetch in function with help of uri segment.
$this->uri->segment(1);

CodeIgniter - When using $route['(:any)'] = 'pages/view/$1' how to use other controllers?

When using
$route['(:any)'] = 'pages/view/$1';
and I want to use other controllers in my routing for example:
$route['del/(:any)'] = 'crud/del';
it won't work. I guess it will use
pages/view/del/$1
and not my crud-controller when deleting an item. How can I solve this?
As indicated, $route['(:any)'] will match any URL, so place your other custom routes before the "catch-all" route:
$route['del/(:any)'] = 'crud/del';
// Other routes as needed...
$route['(:any)'] = 'pages/view/$1';
Its hundred percent working
$route['(:any)'] url is placed last in your routes file
$route['(:any)/company_product_deal_detail'] = "mypage_product_picture/deal_detail/$1";
$route['(:any)/company_service_deals/(:any)'] = "mypage_service_deal_list/index/$1";
$route['(:any)/company_service_deals'] = "mypage_service_deal_list/index/$1";
$route['(:any)'] = "company/index/$1";
I know that it's an old question, but I have found myself a nice solution.
By default, CodeIgniter gives priority to URL's from routes config (even if straight controller, method etc. specified), so I have reversed this priority this way:
In system/core/Router.php find _parse_routes method.
Add this code under literal route match:
$cont_segments = $this->_validate_request($this->uri->segments);
if ($cont_segments == $this->uri->segments) {
return $this->_set_request($cont_segments);
}
I agree, that this approach is kinda wrong, because we edit file from system/core, but I needed a fast soluttion to work with a lot of URL's.

Resources