Calling controller/action is not working as expected? - php

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()
{
}
}

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

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

Why I can not pass a variable between controller functions in codeigniter

I have this in a codeigniter controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Test extends CI_Controller {
public $idioma;
public function index() {
parent::__construct();
// get the browser language.
$this->idioma = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2));
$data["idioma"] = $this->idioma;
$this->load->view('inicio', $data);
}
public function hello(){
$data["idioma"] = $this->idioma;
$this->load->hello('inicio', $data);
}
}
Inicio view:
INICIO <?php echo $idioma ?>
hello view:
Hello <?php echo $idioma ?>
The inicio view works great, but when the hello view is loaded there's nothing displayed.
Any idea why this is not working?
If you wish to set a class property automatically you would do it in the constructor, not in index(). index() does not run before other methods if they are called directly. In your case I assume you're calling hello via the url as test/hello
class Test extends CI_Controller {
public $idioma;
public function __construct(){
parent::__construct();
// get the browser language.
$this->idioma = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2));
}
public function index() {
$data["idioma"] = $this->idioma;
$this->load->view('inicio', $data);
}
public function hello(){
$data["idioma"] = $this->idioma;
$this->load->hello('inicio', $data);
}
}

Navigation with Codeigneter Framework

Basicly I have a contreller that named site.php , and have views:header.php,nav.php,content.php,footer.php etc. problem is how can run content_about.php?
Im trying this url:site/about but I get an error on browser!
Code is that:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class site extends CI_Controller {
public function index()
{
$this->home();
}
public function home()
{
$this->load->view("site_header");
$this->load->view("site_nav");
$this->load->view("content_home");
$this->load->view("site_footer");
}
public function about()
{
$this->load->view("site_header");
$this->load->view("site_nav");
$this->load->view("content_about");
$this->load->view("site_footer");
}
}
Try this one.
public function about()
{
$data=array();
$data['main']='content_about'; //only the content part without header,nav and footer
$this->load->view('template',$data);
}
in view make template.php and put this lines
<?=$this->load->view('site_header.php');?>
<?=$this->load->view('site_nav.php');?>
<?=$this->load->view($main);?>
<?=$this->load->view('site_footer');?>
please let me know if you face any problem.

Passing common title to all view and models CodeIgniter

I have this controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->helper('text');
}
public function index()
{
$this->home();
}
public function home()
{
$data['title']="Somesite";
$this->load->view("view_home", $data);
}
public function blog()
{
$data['title']="Somesite";
$this->load->view("view_blog", $data);
}
public function answers()
{
$data['title']="Somesite";
$this->load->view("view_answers", $data);
}
}
As you may see $data['title'] is same for all functions, how to make it simpler, to include at the beggining and not to write it in every function, repeat again, and then transfer to view.
Is there a way to tranfer it to function?
Before construct function add this:
public $data = array();
Then in the construct function write:
$this->data['title']="Somesite";
And finally before load view add this:
$data = $this->data + $data;
Now you have same $title everywhere.
Here si simple solution and elegant for transfering one variable to all views :)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends CI_Controller {
//Class-wide variable to store stats line
protected $title;
function __construct()
{
parent::__construct();
$data->title = "Some site";
$this->load->vars($data);
}
I'm using this method in every projects.
Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users extends CI_Controller {
//Global variable
public $outputData = array();
public $loggedInUser;
public function __construct() {
parent::__construct();
}
public function index() {
$this->load->helper('url');
$this->load->view('users/users');
}
public function register() {
parent::__construct();
$this->load->helper('url');
$this->load->model('users_model');
//get country names
$countryList = $this->users_model->getCountries();
$this->outputData['countryList'] = $countryList;
$this->outputData['pageTitle'] = "User Registration";
$this->load->view('users/register',$this->outputData);
}
}
View file
<?php if(isset($pageTitle)) echo $pageTitle; ?>
<?php
if(isset($countryList)){
print_r($countryList);
}
?>

Categories