CodeIgniter detect if Default Controller route was used - php

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

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

Get controller action from url, laravel

Can I get the controller action from given URL?
In my project, I will have different layout used for admin and normal users. i.e.
something.com/content/list - will show layout 1.
something.com/admin/content/list - will show layout 2.
(But these need to be generated by the same controller)
I have added filter to detect the pattern 'admin/*' for this purpose. Now I need to call the action required by the rest of the URL ('content/list' or anything that will appear there). Meaning, there could be anything after admin/ it could be foo/1/edit (in which case foo controller should be called) or it could be bar/1/edit (in which case bar controller should be called). That is why the controller name should be generated dynamically from the url that the filter captures,
So, I want to get the controller action from the URL (content/list) and then call that controller action from inside the filter.
Can this be done?
Thanks to everyone who participated.
I just found the solution to my problem in another thread. HERE
This is what I did.
if(Request::is('admin/*')) {
$my_route = str_replace(URL::to('admin'),"",Request::url());
$request = Request::create($my_route);
return Route::dispatch($request)->getContent();
}
I could not find these methods in the documentation. So I hope, this will help others too.
You can use Request::segment(index) to get part/segment of the url
// http://www.somedomain.com/somecontroller/someaction/param1/param2
$controller = Request::segment(1); // somecontroller
$action = Request::segment(2); // someaction
$param1 = Request::segment(3); // param1
$param2 = Request::segment(3); // param2
Use this in your controller function -
if (Request::is('admin/*'))
{
//layout for admin (layout 2)
}else{
//normal layout (layout 1)
}
You can use RESTful Controller
Route:controller('/', 'Namespace\yourController');
But the method have to be prefixed by HTTP verb and I am not sure whether it can contain more url segment, in your case, I suggest just use:
Route::group(array('prefix' => 'admin'), function()
{
//map certain path to certain controller, and just throw 404 if no matching route
//it's good practice
Route::('content/list', 'yourController#yourMethod');
});

Codeigniter - Url Routing

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.

Incorrect routing via /config/routes.php

I have the following (basic) route set up in a CI-based web app:
$route['sms/resend/(:num)/(:any)'] = 'sms/resend/$1/$2';
The controller + 'resend' method:
class Sms extends CI_Controller {
public function resend($to, $message) {
// my code
}
}
Logically speaking, anything that doesn't fit the route should be directed to a 404 page instead of the resend() method within the sms controller. This isn't the case, however. The following URL, for example, isn't redirected correctly, it goes to the same controller+method:
http://myapp/sms/resend/uuuu/WhateverMessage
What could be the problem?
After a bit of digging, I've come to understand that CI's default routing does not get deactivated when a default route related to a specific controller/method pair is added. That being said, if a URL does not fit the route $route['sms/resend/(:num)/(:any)'] = 'sms/resend/$1/$2', then the same URL is run through CI's default routing mechanism as a fallback, so it still takes me to the resend method of the sms controller. To prevent this from happening, I needed to add another custom route that follows all others related to the sms resending, that redirects any other url to a different controller+method. If this controller doesn't exist, you get the default 404 page.
So the final /config/routes.php file:
$route['sms/resend/(:num)/(:any)'] = 'sms/resend/$1/$2';
$route['sms/checkoperator/(:num)'] = 'sms/checkoperator/$1';
$route['sms/(:any)'] = 'somewhereovertherainbow';
I think the rout file is just for rerouting. Your URL don't fits the routing Conditions so it don't gets rerouted! So it goes the normal way wich is the same (in this case!)
Something like this could work!
(! :num) /(:any) '] = error page (or not existing page)
So every request wich doesn't start with a number gets redirected to the error page!
The syntax might be wrong!
This would work great :
$route['sms/resend/[^0-9]/(:any)'] = 'errorpage';
You have to replace the error page by something ;)

Categories