How to get all declared routes in codeigniter? like ex. print_r($route)
Because this is the problem, if the customer registered his username as 'facebook' he will be routed to account/facebook_login and not to his profile, if i changed the order of routes, all links will be routed to customer/profile and this is a no no!
So basically, instead of listing all the routes that i declare and put it into an another array or db table, i want to loop into route array and check if there is a word that has been declared already so that i can stop them to register that word as their username.
this is my sample routes:
// Account routes
$route['login'] = 'account/login';
$route['logout'] = 'account/logout';
$route['register'] = 'account/register';
$route['facebook'] = 'account/facebook_login';
$route['twitter'] = 'account/twitter_login';
$route['settings'] = 'account/settings';
$route['validate/(:any)'] = 'validate/$1';
// Dynamic routes
$route['(:any)'] = 'customer/profile/$1';
From Controller you can do this
print_r($this->router->routes);
It will show all the routes defined in routes.php.
First of all I'm sorry for my English because "I am NOT SCHOOL".
I didn't get much what you are trying to point out. Maybe you want to do similar with this http://www.hirepinoy.com/born2code.
But in my experience with CodeIgniter,it is a bad idea to declare $route['(:any)'] = 'customer/profile/$1'; in your routes.
I think the best option you can do is that to create a class to check if username exist in the table of users by using HOOK see http://codeigniter.com/user_guide/general/hooks.html.
So therefore when username (unique field) returned then you can modify the $_SERVER['REQUEST_URI'] to be like this
$_SERVER['REQUEST_URI'] = '/customer/profile/'.$username;
So basically it will modify the SERVER REQUEST before the codeigniter core process it's core processing.
Now, the problem maybe, is when user registered a username that is the same with your controller for sure will not be process since it was modified to route on costumer/profile/blahblah. All you need to do is to create a custom validation to check weather the username already exists on database and or your controller name.
You can do like
if (file_exists(APPPATH."controllers/{$value}.php")) {
$this->CI->form_validation->set_message('is_unique', 'Username is already taken');
return FALSE;
}
Related
I am having entry in my route.php like - $route['admin/students'] = 'view_student'. Here view_student is controller name. Now when from "localhost/school/admin" page I call Students, than everything works fine; But when I change my route like - $route['/school/admin/students'] = 'view_student', and call it from "localhost/school/admin" page as Students, than 404 page is shown. Whats wrong in here?
Try this code it might help you :
Here dashboard is the name of controller
//this will route as localhost/appFolder/admin/index
$route['admin'] = 'dashboard'; // for your index page
//this will route as localhost/appFolder/admin/method_name
$route['admin/(:any)'] = 'dashboard/$1';
//this will route as localhost/appFolder/admin/method_name/param1
$route['admin/(:any)/(:any)'] = 'dashboard/$1/$2';
Link the route Like
// for your index page
// for your other pages
To link the other controller defined just like
school is your ci root, so if you define $route['/school/admin/students'], it will seek school class with admin function, that never exist, instead of admin route.
you should read the documentations first before make any step,
https://www.codeigniter.com/userguide3/general/routing.html
i will make a project management web app. if the user register the system will give a URL.
www.site.com/company_name
how should i do it when he user used this url it will also check in the database if it exist?
in codeigniter the format should be www.site.com/controller/function
If this is about Routing then you may create a Controller i.e. Profile to retrieve the user according to company_name passed in to the url, in this case you may route it like
// application/config/routes.php
$route['(:any)'] = 'profile/get_user/$1';
In this case, when a url like www.site.com/microsoft is given, this will be routed to Profile controller and will call the get_user method and microsoft will be passed to the method as it's parameter. So, your controller should look something like this
class Profile extends CI_Controller {
public function get_user($company_name = null)
{
// Check if $company_name exists or not and do something with it
// Query for the user in the appropriate table
// and search using $company_name (make sure this field is unique)
}
}
Also, you can use a route like this
$route['([a-zA-Z0-9]+)'] = "profile/get_user/$1";
Also, remember, a url with www.site.com/john could also be routed to profile/get_user/john instead of User/show/john if you have a controller/method like this. Read more on URI Routing.
In CodeIgniter, is there a way to know if a user was sent to the Default Controller because the route sent them there, OR because the user actually entered that controller in the URL bar.
In other words, ---.com/home and ---.com could both send you to the 'home' controller, because you have set
$route['default_controller'] = 'home';
But only ---.com/ would invoke CI to fetch the "default_controller"
So, how do I detect this? If only there was a boolean function that could tell me this.
You should be able to use $this->uri->total_segments() ... or one of the other functions in the URI class to deduce this ...
if($this->uri->total_segments() === 0){
//user came in by default_controller
}
URI Class Docs
I'm using codeigniter framework and by default i load my site controller
$route['default_controller'] = "site";
But now i want to setup the routes.php config so i could still access my controllers as before but if controller doesent exists then i'd like to run users controller and check if it's nickname so i can display that users profile.
It's something like on Facebook where you can have www.facebook.com/username and it takes you to your user profile. But i'd like that my other controllers are still accessible www.mysite.com/site, www.mysite.com/site/function, etc
I have tried with :any wildcard but couldnt get it working. I have seen that some solve the problem with regex expression but i'm not good at regex and dont know how to put it together so it would work for me.
Try my way:
First, Set $route for each controller that you have, exmp:
$route['controller_1'] = 'controller_1';
$route['controller_1/(:any)'] = 'controller_1/$1'; //this let you access your method
$route['controller_2'] = 'controller_2';
$route['controller_2/(:any)'] = 'controller_2/$1'; //this let you access your method
Next, use :any
$route['(:any)'] = 'user_controller/$1';
good luck !!!
Try this way. it will reduce a lot of repetitive line if you have lots of controller but i don't know does it violate any CI rules.
//this code block should be placed after any kind of reserved routes config
$url_parts = explode('/',strtolower( $_SERVER['REQUEST_URI']) );
$reserved_routes = array("controller_1", "controller_2", "controller_3" );
if (!in_array($url_parts[1], $reserved_routes)) {
$route['([a-zA-Z0-9_-]+)'] = "controller_1/profile/$1";
}
On my website, I am loading the content dynamically from database like this
e.g mysite.com/about-us
for this, there is an enrtry in database, so it will load the content for 'about-us' & print it using "page" controller only.
for this what I have done is, I have added below configuration in routes.php
$route[':any'] = "page";
but lets say if I already have controller named "about-us" and I want to load that & not the one from database, how can I do that?
A smooth solution would be to use the error/missing_page controller and point it in the config/routes.php.
Then it would automaticly pick all existing controllers first, and then that controller.
You can also call show_404() if you don't find a record in the database.
This allows you to create new controllers without having to point all of them in the route file.
Read about 404 override here
you need to add this
$route['about-us'] = "aboutus";
$route['about-us/(:any)'] = "aboutus/$1";
before
$route[':any'] = "page";
as the CI route is not greedy, it will not check for the page controller after it finds the about-us controller.