Codeigniter - Url Routing - php

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.

Related

static Url rewriting in Codeigniter

I have a codeigniter application in my localserver.
I need some URL rewriting rule for the following.
If any one have any solution please answer.
http://localhost/myapp/index.php/users/login
to
http://localhost/myapp/index.php/usuarios/login
How can I do this in Codeigniter.
This record in config/routes.php will do what you want -
$route['users/login'] = 'usuarios/login'; // for login only (static)
$route['users/(:any)'] = 'usuarios/$1'; // for all routes to users (dynamic for functions without parameters)
More details here : URI Routing
Codeigniter works like this to access myFunction from Mycontroller with the value myVarValue
http://my.webPage.com/index.php/MyController/myFunction/myVarValue
U want to alter the URL, yes you can by creating routes on ../application/config/routes.php
$route['my-function-(:any)'] = 'MyController/myFunction/$1';
This is the first step
You created the routes and those are not working?
Set the base_url on the file ../application/config/config.php, in your case will be http://localhost/myapp, line number 26 of the file on CI v3.1.6
Set the default controller, the default controller that will be inmediatelly accessed by anyone who enters ur website, u set this on ../application/config/routes.php, your default controller can be the name of a Controller or A function within a controller.
Let's say u have this controller
public class MyController extends CI_Controller{
public function __construct(){
parent::__construct();
}
public function index(){
echo 'Hello people, im the index of this controller,'.
' im the function u will see everytime u '.
'access the route http://localhost/myApp/MyController';
}
public function notTheIndex(){
echo 'Hello, im the function u will see if u'.
'access http://localhost/MyController/notTheIndex';
}
}
Lets say this controller will be ur default controller so as mentioned in the routes.php file in the line 53 on ci v3.16 set
$route['default_controller'] = 'MyController';
//if u want to just access the index method
$route['default_controller'] = 'MyController/notTheIndex';
//if for some reason u have a method u would like to be executed
//by default as the index page
Now u want the uris to keep have the names u set
$route['some-beautiful-name'] = 'MyController/notTheIndex';
so when u access http://localhost/some-beautiful-name it will show the result of MyController/notTheIndex
Consider that if u are accessing the different pages via a <a href='some_link'>Some link</a> and the href values need to be changed to your defined routes, if not, this will still work if they are pointing to controllers, but the uri names will stay the same, lets take the example of 'some.beautiful-name' to access MyController/notTheIndex
In Your View
This is a link
The link will work but the uri name will be http://localhost/MyController/notTheIndex
This is a link
The link will work because u defined a route like this else it will not and show 404 error, but here the url shown will be http://localhost/some-beautiful-name
Hope my answer helps u.

How to achieve this kind of url routing in codeigniter

i want to achieve url routing something like www.example.com/alicia
suppose alicia is not a class name or method name just something like passing data in url and with some class i want to access this and want to use it for further process.How i can use it ? Thanks in advance.
You can use Codeigniter's built-in routing, the file route.php is located in your config folder.
there you can add:
$route['alicia'] = 'welcome/index/something';
$route['alicia/:any'] = 'welcome/index/someotherthing/$1';
then in your controller, for example welcome you just create a function like:
public function index($page = null){
if($page=='something'){
// do what you need to do, for example load a view:
$this->load->view('allaboutalicia');
}
elseif ($page=='someotherthing'){
// here you can read in data from url (www.example.com/alicia/2017
$year=$this->uri->segment(2); // you need to load the helper url previously
}else{
// do some other stuff
}
}
documentation on routing and on urlhelper
edit after comment:
in case your uri segment is representing a variable, like a username, then you should use a uri scheme like www.example.com/user/alice and create your route like:
$route['user/:any'] = 'welcome/index/user';
then in your controller welcome
public function index($page=null){
if($page=='user'){
// do what you need to do with that user
$user=$this->uri->segment(2); // you need to load the helper url
}
else{
// exception
}
}
This could be tricky because you don't want to break any existing urls that already work.
If you are using Apache, you can set up a mod_rewrite rule that takes care to exclude each of your controllers that is NOT some name.
Alternatively, you could create a remap method in your base controller.
class Welcome extends CI_Controller
{
public function _remap($method)
{
echo "request for $method being handled by " . __METHOD__;
}
}
You could write logic in that method to examine the requested $method or perhaps look at $_SERVER["REQUEST_URI"] to decide what you want to do. This can be a bit tricky to sort out but is probably a good way to get started.
Another possibility, if you can think of some way to distinguish these urls from your other urls, would be to use the routing functionality of codeigniter and define a pattern matching rule in the routes.php file that points these names to some controller which handles them.
I believe that the default_controller will be a factor in this. Any controller/method situations that actually correspond to a controller::method class should be handled by that controller::method. Any that do not match will, I believe, be assigned to your default_controller:
$route['default_controller'] = 'welcome';

Codeigniter Routing

I'm trying to write an application that takes a parameter from the url and supplies it to the index function in the default controller. Then decides what to load depending on this parameter either a default home page or a custom page.
$route['(eventguide/:any)'] = 'eventguide';
This code works but only if I have the controller in the url like so:
example.com/eventguide/(parameter)
I don't want to include the controller name. So i'm not exactly sure how to route this.
Ideally the url would look like example.com/(parameter), is this possible?
Yes, you're almost there:
$route['(:any)'] = "eventguide/index/$1";
And in your index() method you fetch the parameter:
public function index($parameter = null){
}
$parameter will now contain anything caught by the :any shortcut, which should be equivalent to (\w)+ IIRC
Since this is a catch-all route, be careful to put any other custom route you want before it, otherwise they will never be reached.
For ex., if you have a controller "admin", your route files should be;:
$route['admin'] = "admin";
$route['(:any)'] = "eventguide/index/$1";

CodeIgniter detect if Default Controller route was used

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

codeigniter get all declared routes

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;
}

Categories