How to call Module function in Codeigniter HMVC? Tank auth - php

I have installed tank auth to save me time creating a authentication script.
As I am using HMVC, tank auth has it's own module (modules/auth).
How can I protect my other modules (/admin, /members etc) with the login script??
From what I have read I need to do something like:
modules::run('auth/is_logged_in');
My auth controller looks like:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Auth extends MX_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
//$this->load->library('security');
$this->load->library('tank_auth');
$this->lang->load('tank_auth');
}
function index()
{
if ($message = $this->session->flashdata('message')) {
//$this->load->view('auth/auth/general_message', array('message' => $message));
$main_content = 'auth/auth/general_message';
$this->load->view('includes/template', array('message' => $message, 'main_content' =>$main_content));
} else {
redirect('auth/login/');
}
}
/**
* Login user on the site
*
* #return void
*/
function login()
{
if ($this->tank_auth->is_logged_in()) { // logged in
redirect('admin');
} elseif ($this->tank_auth->is_logged_in(FALSE)) { // logged in, not activated
redirect('auth/send_again/');........
an the controller/module I want to protect with the login script:
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Admin extends MX_Controller {
public function __construct(){
parent::__construct();
modules::run('auth/auth/login');
}
The above does not seem to work? What am I missing?

I put this code into my controllers:
function _remap($method)
{
if (method_exists($this, $method) && $this->tank_auth->is_logged_in())
{
$this->$method();
}
else
{
redirect('/auth/login/');
}
}

Related

using codeigniter,admin wants to approve user's registration request. But itsn't happening properly

here is my controller page..
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class ApproveUser extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('admin/Approve_Model');
}
public function index()
{
$this->load->model('admin/Approve_Model');
$result['data']=$this->Approve_Model->getView();
if(!empty($this->session->userdata('user_id')))
{
$this->load->view('admin/header');
$this->load->view('admin/approveUser',$result);
$this->load->view('admin/footer');
}
else{
redirect(site_url('Pages/index'));
}
}
while running the code, it is redirecting to my index page
Try this:
if(!isset($this->session->userdata('user_id')))
{
$this->load->view('admin/header');
$this->load->view('admin/approveUser',$result);
$this->load->view('admin/footer');
}
else
{
redirect(site_url('Pages/index'));
}

CodeIgniter routes issues to access the frontend and backend folder

I am using CodeIgniter. I have frontend and backend folder inside controllers and views. I tried server steps even check almost all the solution but still I am not able to access it my default controller
routes.php
$route['default_controller'] = 'frontend/User_control';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
/*backend*********************************/
$config['backend'] = 'backend/Access_control';
1) My issue is When I am accessing the Url http://localhost/example_ci_row/
I am getting 404 Page not found
2) How to access the backend URL I tried http://localhost/icube_row/admin
but I am getting the error
frontend User_control
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User_control extends CI_Controller {
public $current_date;
function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('form');
}
public function index(){
$this->load->view('frontend/login');
}
}
?>
Backend Access control
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Access_control extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('form');
}
public function index(){
$this->load->view('backend/login');
}
}
?>
Edited
It's working when I use below steps. I added the Test.php file in the controller and change the routes then I am getting the login page.
Test.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller {
public $current_date;
function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('form');
}
public function index(){
$this->load->view('frontend/login');
}
}
?>
routes.php
$route['default_controller'] = 'Test';
Hope this will help you :
The built in $route['default_controller'] will not work for sub-folders. you have to extend system router as per your requirements like this :
You need to create a MY_Router.php in application > core > MY_Router.php
<?php
class MY_Router extends CI_Router {
protected function _set_default_controller() {
if (empty($this->default_controller)) {
show_error('Unable to determine what should be displayed. A default route has not been specified in the routing file.');
}
// Is the method being specified?
if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2) {
$method = 'index';
}
// This is what I added, checks if the class is a directory
if( is_dir(APPPATH.'controllers/'.$class) ) {
// Set the class as the directory
$this->set_directory($class);
// $method is the class
$class = $method;
// Re check for slash if method has been set
if (sscanf($method, '%[^/]/%s', $class, $method) !== 2) {
$method = 'index';
}
}
if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php')) {
// This will trigger 404 later
return;
}
$this->set_class($class);
$this->set_method($method);
// Assign routed segments, index starting from 1
$this->uri->rsegments = array(
1 => $class,
2 => $method
);
log_message('debug', 'No URI present. Default controller set.');
}
}
This will allow you to use $route['default_controller'] = 'frontend/User_control'; as your default controller
Have you configured the Base URL ?
Application->config->config.php
$config['base_url'] = 'http://localhost/example_ci_row';

Calling controller/action is not working as expected?

Click link as controller/function (signout/signout) is not working .. I mean if I click link controller/action it's not going to that called function.But controller/action/action (signout/signout/signout) is working in my CI framework..
//not working
echo "<div id='menu'><ul><li>Signout</li></ul></div>";
//working
echo "<div id='menu'><ul><li>Signout</li></ul></div>";
MY CONTROLLER Signout.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Signout extends CI_Controller {
public function __construct() {
// Call the CI_Model constructor
parent::__construct();
$this->db = $this->load->database('default', true);
}
public function index() {
}
public function signout() {
$this->load->view("signout_signout");
}
}
Here I want controller/action(signout/signout) to work ! Because 2 worked method url(signout/signout/signout) is a little long ,so don't like it . How can i manage that?
Call signout() from your index() action. Change:
public function index() {
}
to
public function index() {
$this->signout();
}
or use this and eliminate signout() altogether:
public function index() {
$this->load->view("signout_signout");
}
It was working fine as i check your code.
Try test this code and see
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller
{
function __construct()
{
parent::__construct();
}
public function welcome()
{
echo "<div id='menu'><ul><li>Signout</li></ul></div>";
echo "<div id='menu'><ul><li>Signout</li></ul></div>";
}
public function signout()
{
echo "signout";
}
public function index()
{
}
}

Best Location to write the Session code to check if Session Exists

Using : PHP MVC CI
I created a Controller class with the name of My_Controller in Core Folder. In this class, It is being checked if the session exists or not. This controller is being extended in all controller classes. Here is the code
<?php
class My_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->library('session')
if(empty($this->session->userdata('userName')) {
header('Location: '."Login Url");
}
}
}
?>
Question: Is there any better location to write the Session code in MVC Architecture ??
You don't have to use session_start();
In Controller
class My_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
}
public function index($value)
{
$session = $this->model_name->check_session();
if($session==true){
#valid code
}
else{
redirect('controller/method');
}
}
}
In Model
public function log_in()
{
$log = $this->session->all_userdata();
if (isset($log['userName'])) {
return true;
} else {
return false;
}
}
you need to create the hook in CI and check the session over there.
Hook will call in every request so you can write permission role over there.
Here is more detail how you can write the hook.
http://www.codeigniter.com/user_guide/general/hooks.html
create a file name auth_hook.php in hook folder and write this code
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Auth_hook extends CI_Controller {
private $CI;
public function __construct() {
$this->CI = & get_instance();
}
public function is_authorized() {
$uri = $this->CI->uri->segment(1);
if (strcmp($uri, 'user') && $uri != '') {
if ($this->CI->session->userdata('logged_in')) {
return true;
} else {
redirect(site_url('user'));
}
return true;
}
return true;
}
}

What is wrong with my Model and Controller?

For the life of me I cannot understand why I am receiving errors with my install of CodeIgniter and this current MVC set up.
The error I see is
Fatal error: Call to undefined method Login::users_model() in /var/www/application/controllers/login.php on line 17
Controller application/controllers/login.php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$data = '';
$this->load->model('users_model');
$data['testing'] = $this->users_model()->test_user();
$this->load->view('home',$data);
}
}
Model application/models/users_model.php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users_model extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function test_user() {
return 'Test User #1';
}
}
View application/views/home.php
echo $testing;
No need for function bracket with model name
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$data = '';
$this->load->model('users_model');
$data['testing'] = $this->users_model->test_user();
$this->load->view('home',$data);
}
}
Replace $data['testing'] = $this->users_model()->test_user(); with $data['testing'] = $this->users_model->test_user(); in your controller.
Simply load your model inside the constructor
public function __construct()
{
parent::__construct();
$this->load->model('users_model');
}
Then simply call the relevant functions inside any controller functions like this.
public function index()
{
$data = '';
$data['testing'] = $this-> users_model-> test_user();
$this->load->view('home',$data);
}

Categories