Forgive me if this is a "duh" question or if you need more information to answer, I am new to CodeIgniter and still haven't figured out a few things with best practices and such...
In routes.php I have $route['default_controller'] = "home"; so my default_controller is obviously "home".
Inside my home.php controller I have:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
function __construct() {
// blah
}
function index(){
// blah
}
function login(){
// blah
}
}
Which works fine and everything, there is no problems with that. The only thing I can't figure out is if I want to access the login function, I currently have to go to www.blah.com/home/login. How can I change it so it just goes to www.blah.com/login, without creating a new controller (I would like to keep some of these one time base urls all in my default controller)? Is that even possible or do I just have to create a new controller?
If I just have to create a new one, is there a best practices for how many controllers you have, etc.
Documentation says: This route will tell the Router what URI segments to use if those provided in the URL cannot be matched to a valid route.
So use $route['login'] = 'home/login';
The way I have it set up is to have the index function act as the gatekeeper: if not logged in, show a login form view, if logged in, redirect to your (protected) home page. The login function is only accessed via post, when the user submits his login/pwd, and performs the login logic.
You need add a line to your application/config/routes.php file
$route['login'] = "home/login";
Related
I've been researching up on how to add views and I'm stumped. I want to add a view and all I get are 404 errors. All the examples I see on the web are just to add a default controller. I have a default controller, now I want to add a new page passed an ID in the URL.
This is the controller xyz.php:
class Xyz extends CI_Controller {
public function index() {
date_default_timezone_set('UTC');
$this->load->model('xyz_model');
$this->load->view('xyz_main_view');
}
public function activity() {
date_default_timezone_set('UTC');
$this->load->model('xyz_model');
$this->load->view('xyz_activity_view');
}
}
Model is xyz_model.php, and views are xyz_main_view.php and xyz_activity_view.php.
This is routes.php:
$route['default_controller'] = 'xyz'; // works okay
// $route['xyz'] = 'xyz/activity'; // 404
// $route['activity'] = 'xyz/activity'; // 404
// $route['xyz/activity'] = 'xyz/activity'; // 404
// ... many, many other different approaches
I'm able to use http://localhost, but I'd like to use the following:
// map to main view
http://localhost/index
http://localhost/xyz
http://localhost/xyz/index
// map to activity view
http://localhost/activity
http://localhost/xyz/activity
My understanding is that some of the URLs for the main view should work automatically, not seeing it. Just http://localhost.
I haven't even touched how to get an ID from the URL for the activity page. Just want to get over this first hurdle.
Keep this code in routes
$route['default_controller'] = 'xyz';
Then Try this URL to execute "activity()" function in xyz controller.
http://localhost/[YOUR PROJECT FOLDER NAME]/index.php/xyz/activity
To Pass Parameters such as id's you can use this url.
http://localhost/[YOUR PROJECT FOLDER NAME]/index.php/xyz/activity/[ID]
For more information please check codeigniter routing library. It is really easy to understand.
https://www.codeigniter.com/user_guide/general/routing.html
I have a CI Project where I want to change the URL like below:
www.example.com/index.php/Controller/method/param1
to
param1.example.com/index.php/Controller/method
Is this possible using just CI? Like routes. Or we need to .htaccess?
If possible, are there any risks or is it a bad practice?
Also does it affect SEO?
EDIT: This rewrite should happen only for Controller controller.
EDIT: I think the meaning came out wrong. When a user enters a URL like
param1.example.com/Controller/method It has to fire the method in Controller and pass param1 as parameter.
You need to create redirect controller (controllers/Redirect.php), that will redirect browser Something like this:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Redirect extends CI_Controller {
public function index($method,$domain) {
$redirect_url = 'http://www.' . $domain . '.example.com/index.php/Controller/' . $method;
redirect($redirect_url,'refresh');
}
}
?>
Add this line in application/config/route.php
$route['Controller/(:any)/(:any)'] = 'redirect/index/$1/$2';
You are done. Enjoy :)
I am new to codeigniter and really interested in trying it out. I followed the guide but ran into a problem. It seems like I am unable to load my first page properly.
I have inside the view folder another folder called general and inside it index.php.
in controller folder, I have sub-folder with general and inside it is Default controller.
I have the following route and yet the page showing is blank
$route['default_controller'] = "general/default";
$route['404_override'] = '';
When I visit the link, I type this in browser:
http://localhost:8888/treventa/
and the screen is blank. Am I doing something wrong? Sorry if this is too simple but a person got to learn from his mistake :)
Try with me step by step:
Firstly: the Controller:
(main.php) File content
if (!defined('BASEPATH'))exit('No direct script access allowed');
class Main extends CI_Controller {
public function index() {
$this->load->view('general/welcome');
}
}
Secondly: The view:
(welcome.php) File content
You can put anything you want
<h1> Hello, I'm the view file </h1>
Finaly: The routes:
(routes.php) File content
$route['default_controller'] = "general/main";
Now call the script like this http://localhost/codeIgniter/, that's where codeIgniter is the script folder name.
I think everything now is clear.
CI trying to use method default() of general controller. If You want to use folders for controllers, make sure You specify method, so try $route['default_controller'] = "general/default/index";
And one more thing: this method (default_controller) will be used when You trying reach Your root http://localhost:8888/, if You want to route request from http://localhost:8888/treventa/ to default controller, You need to add route-rule to /config/routes.php something like $route['treventa'] = "general/main/index";
In my CI application I have a few controllers to handle different user-type functions:
CA
CD
DV
CSC
CB
So currently when someone logs in, he is redirected by his role to (example) : localhost/CA or localhost/CD etc..
I need to rewrite the routes to redirect everything depending on his role:
$route['(:any)'] = 'CA/$1'; (CA should not be hardcoded)
The rule should also be removed when using the login Controller (by filtering some url's)
Can anyone show me how to hook the rules after login? and also how to use a regexp to filter some url on which to apply the rules?
$route['^((?!auth/).)*$'] = '$1';
What other way would be to achieve this? .htaccess is out of the question since I need some data logic to create the routes.
Thanks to this wonderfull tutorial about routing from the database, I managed to add new routes after the user logs in (in my Auth controller):
class Auth extends CI_Controller {
function Auth() {
parent::__construct();
}
function index()
{
if ($this->ion_auth->logged_in())
{
$this->__checkRoles();
}
function __checkRoles()
{
$role=$this->ion_auth->get_group();
$this->save_routes($role);
redirect(base_url().'index');
}
public function save_routes($controller=null)
{
$output="<?php ";
//some uri's don't need routing
$output.="\$route['auth']='auth';";
$output.="\$route['auth/(:any)']='auth/$1';";
$output.="\$route['rest']='rest';";
$output.="\$route['rest/(:any)']='rest/$1';";
//for the rest route trough the "user-type" controller
$output.="\$route['(:any)']='".$controller."/$1';";
$this->load->helper('file');
//write to the cache file
write_file(APPPATH . "cache/routes.php", $output);
}
My routes.php looks like this:
$route['404_override'] = '';
$route['default_controller'] = "auth";
include_once(APPPATH."cache/routes.php");
It sounds like what you want is to implement remapping: the standard routing system is not designed to let you alter the routes based on whether or not the user is logged in.
However, you MIGHT (I've never seen this working) be able to put conditional statements in the standard routes.php file that checks to see if the user is logged in and what their role is (look at a session cookie or something like that) and then load different route options. Never tried it, but that MIGHT work for you.
I have a site using CodeIgniter that is almost complete now. My problem is that, even though I have implemented sessions and maintain a login system, a person can access any page by typing the URL into the browser address bar.
I have implemented the session for patient registration like this:
function index(){
$this->is_logged_in();
}
function log_out(){
$this->session->sess_destroy();
redirect('login_controller');
}
function is_logged_in(){
$is_logged_in = $this->session->userdata('is_logged_in');
if(!isset($is_logged_in)||$is_logged_in!= TRUE ){
redirect('login_controller');
}else{
$this->main();
}
}
Anonymous users can't acess the system just by typing the controller name like this:
http://localhost/demo_site/index.php/register_controller
But they can do it like this:
http://localhost/demo_site/index.php/register_controller/search_patient
Person can't access by typing the controller name, but can enter the system by typing a longer url than the controller, like the one shown above.
What is the problem here? What are the possible solutions??
You will have to implement a login check in the controller's constructor.
Whenever the controller is called, it should check if the user is logged in - if they are not, redirect to a login page or an error page.
To confirm if it is entering the login check put an echo and exit inside the is_logged_in() function and check if it appears in case of http://localhost/demo_site/index.php/register_controller/search_patient
You are probably doing login check in your respective modules and thus you missed for some cases.
It is better to define a set of private modules (say in an array) and do the login check in the frontcontroller itself (in one place) instead of repeatedly in module level.
Sounds like a routing problem. You need to set up your routes to make the second case illegal or at least map to the same controller as the first case. More on routing here.
I agree with tHeSiD. This code should go in the constructor. Ideally in a base class which you use to extend all admin related or restricted classes with. Normally I use an Admin_Controller base class that extends CI_Controller (2.0) or Controller (1.7.x) and then create my application controllers by extending the Admin Controller.