I have a question which regards to controllers. Let's get it started:
I have a main controller named "admin.php", it has a menu for company, user management, etc. Each item on the menu has separate PHP file to hold different kinds data [I seems to be lengthy to combine them all in one php.
So for this example:
I have 3 controllers: admin.php , company.php, usermanagement.php
What I want is, link the company and management controllers as a child of admin. So if enter the address on the browser, it may look: localhost/admin/company and localhost/admin/usermanagement
I configured the routes and it's good but when I enter "localhost/company" it loads the company page which i didn't want to. i wanted to link them all as a child of an admin page.
How would I achieve this?
by the way here's a snippet of my code:
admin.php - Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Admin extends CI_Controller {
function __construct(){
parent::__construct();
session_start();
}
public function index() {
$this->load->view('view_admin');
}
}
Company - Same as the admin
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Company extends CI_Controller {
function __construct(){
parent::__construct();
session_start();
}
public function index() {
$this->load->view('view_company');
}
}
Thanks,
James
EDIT: I have tried adding functions on the admin.php like:
function company() {}
function usermanagement() {}
but I guess it wasn't that effective since it will include lots of functions later on as I try to migrate my native php codes into this MVC architecture framework.
If your issue is that you like the way routes work but don't want people to be able to visit index.php/company/ and rather prefer that they visit admin/company you can always do:
class Company extends CI_Controller {
public function __construct() {
parent::__construct();
if ( $this->uri->segment(1) != "admin" ) {
redirect('admin/company/'.$this->uri->segment(3));
}
}
...
Although bear in mind that you would probably need a more complete URL forming method than just adding $this->uri->segment(3) but the general pattern is there.
You can add more functions in your 'admin' controller, so, default page is:
public function index() {
$this->load->view('view_admin');
}
'Subpage' is:
public function company() {
$this->load->view('view_company');
}
etc, etc...
Related
in codeigniter I have my main controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Main extends CI_Controller
{
public function index()
{
$this->load->library('../controllers/forum');
$obj = new $this->forum();
$obj->test();
}
}
And the controller I'm trying to access:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Forum extends CI_Controller
{
function __construct()
{
echo "testing1";
$this->load->library('session');
parent::__construct();
$this->load->database();
$this->load->model('model_forum');
}
public function index(){
}
public function test(){
echo "testing2";
$this->data['forums'] = $this->model_forum->getForums();
$this->load->view('homepage', $this->data);
}
}
Everything is fine with my model_forum.php file, because it works if I put all the code in Main controller. But if I'm trying to access Forum controller, nothing works, only "testing1" echo goes through. Picture of error:
Anyone has any idea what I'm doing wrong? I'm new to PHP and codeigniter so I'm struggling a little bit. Thanks in advance.
You can't load a controller from a controller in CI - unless you use HMVC or something.
You should think about your architecture a bit. If you need to call a controller method from another controller, then you should probably abstract that code out to a helper or library and call it from both controllers.
UPDATE
After reading your question again, I realize that your end goal is not necessarily HMVC, but URI manipulation. Correct me if I'm wrong, but it seems like you're trying to accomplish URLs with the first section being the method name and leave out the controller name altogether.
If this is the case, you'd get a cleaner solution by getting creative with your routes.
For a really basic example, say you have two controllers, controller1 and controller2. Controller1 has a method method_1 - and controller2 has a method method_2.
You can set up routes like this:
$route['method_1'] = "controller1/method_1";
$route['method_2'] = "controller2/method_2";
Then, you can call method 1 with a URL like http://example.com/method_1 and method 2 with http://example.com/method_2.
Albeit, this is a hard-coded, very basic, example - but it could get you to where you need to be if all you need to do is remove the controller from the URL.
You could also go with remapping your controllers.
From the docs: "If your controller contains a function named _remap(), it will always get called regardless of what your URI contains.":
public function _remap($method)
{
if ($method == 'some_method')
{
$this->$method();
}
else
{
$this->default_method();
}
}
Okay. So I have been stumping myself on where I have gone wrong with this. I am not-so-new to PHP but I am also no expert, although I am getting more and more used to OOP. I have recently got into CodeIgniter 3. I like it, I use it, it's pretty simple to grasp in the beginning.
However, I am in the learning stage where I cannot successfully pass dynamic data, from the database into the controller using a Model.
And just as an FYI: I am using MY_Controller as my Base Controller, and have set a $data object to pass variables application wide. I know my problem is not due to the use of MY_Controller, because when I use static $this->data['KEY'] = 'VALUE'; or $this->data array('KEY' => 'VALUE');, my variables get passed into my views. So MY_Controller is functional.
My problem resides in creating a Settings_model. Its sole purpose is to take data from a database table settings and extrapolate certain columns of data, such a/an: application title, background color, etc.
So. On to my question/s, and what I have tried to do to fix this.
First, here is my MY_Controller:
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
public $data;
public function __construct() {
parent::__construct();
$this->data = array(
'app_title' => $this->settings_model->get_title('$title'),
'meta_author' => APP_AUTHOR
);
}
}
And here is my Settings_model:
defined('BASEPATH') OR exit('No direct script access allowed');
class Settings_model extends CI_Model
{
public $title = '';
public function __construct() {
$this->load->database();
}
public function get_title($title) {
$this->db->select('app_title');
$this->db->from->('settings');
$this->db-where('app_title', $title);
return $this->db->get()->row('app_title');
}
}
My Problem
When using the sytnax above. i am able to see my applications output in the browser. Everything is rendered properly, all the static variables I've assigned in MY_Controller work. Everything is good ... except, the $app_title variable (which is in my views, and assigned in MY_Controller) does not load the data from the database, as the model instructed. As well as does not produce any error, warning or notice.
What I've tried
Man i tell you I have tried everything. i have tried inserting the query into MY_Controller directly and not using a Model (which I know is not proper MVC, but I wanted to try), like so:
MY_Controller Using Straigh Query, no model
$this->data = array(
'app_title' => $this->db->query('GET app_title FROM settings'),
'meta_author' => APP_AUTHOR
);
And the one time i got somewhere, error/notice wise was when i altered the Settings_Model, like so:
Altered Settings Model
defined('BASEPATH') OR exit('No direct script access allowed');
class Settings_model extends CI_Model
{
public $title = '';
public function __construct() {
$this->load->database();
}
public function get_title($title) {
$this->db->select('app_title');
$this->db->from->('settings');
$this->db-where('app_title', $title);
// Before it was this:
return $this->db->get->row('app-title');
// But I changed it to this
return $this->db->get()->row()->app_title;
}
}
But that syntax above gave me the error in this Image of a non Object Error
I do not know what I am doing wrong.
Now the question/s
Why when i use the first approach of the settings_model, using return $this->db->get()->row('app_title'); does it show all of my applications output in the browser, but everywhere the $app_title variable is called, it itself displays no value from the database, via MY_Controller and Settings_model?
Why when i use the second approach of the settings_model, using return $this->db->get()->row()->app_title; does it produce the Trying to get property of Non Object error in the image i linked to above?
What on earth am I doing wrong? I know I am still on the early learning stages of OOP, and Models, etc. But I must be missing something, I know something is not right.
Any and all help is greatly appreciated and welcomed. I thank everyone for their input.
EDIT 1: Thanks to #Mirceac21 for asking. I am autoloading the settings_model in config/autoload.php.
EDIT 2: I have now switched the var $data to public $data in MY_Controller as #Mirceac21 suggested. Still the same error but he is correct about not using var there.
Okay, so I figured it out. With the help of #Mirceac21. I change the var $data to public $data in MY_Controller.
Then in the Settings_model, I took out the WHERE clause and it worked. Here is the new working code below:
MY_Controller
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
// $data object used for $this->data
public $data;
public function __construct() {
parent::__construct();
/*
* The $data array('KEY' => 'VALUE') variable sets are used
* in other Controller Classes that extend MY_Controller.
*/
$this->data = array(
'app_title' => $this->settings_model->get_title('$title'),
'meta_author' => APP_AUTHOR
);
}
}
Then in the Settings_model I made the following changes, remove ->where('app-title', $title);. Like so:
defined('BASEPATH') OR exit('No direct script access allowed');
class Settings_model extends CI_Model
{
public $title = '';
public function __construct() {
$this->load->database();
}
public function get_title($title) {
$this->db->select('app_title');
$this->db->from->('settings');
// I removed the following where->(); and it now loads the dynamic data
// $this->db-where('app_title', $title);
return $this->db->get()->row('app_title');
}
}
Now everything works as planned. I knew it was something simple and stupid 9for lack of better words). Thank you to #Mirceac21 for your input.
I'm building a Codeigniter application with ION Auth, which is going relatively OK. I've set up the ION Auth framework following this guide http://www.rappasoft.com/tutorials/5-building-a-simple-codeigniter-application-using-ion-auth.html#.U0Q5ufldUrU and when I log in using the default username and password I'm redirect to my homepage. So far, so good.
Problem then is that when I access one of my protected pages (i.e. a controller which extends MY_Controller), I'm redirected to the login page again.
I'm quite new to this, so just looking for some pointers of where to look. It seems I'm being logged in, but I don't really know how to check for sure.
I've tried this line of code in my view, but I get a couple of errors: user_info is undefined & trying to get property of a non object.
<?php echo $user_info->username;?>
MY_Controller in application/core:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
if (!$this->ion_auth->logged_in()) {
redirect('auth/login');
} else {
//Store user in $data
$data->user_info = $this->ion_auth->user()->row();
//Load $the_user in all views
$this->load->vars($data);
}
}
}
My 'members only' area controller class declaration:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Manage extends MY_Controller {
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->library('grocery_CRUD');
}
//other functions omitted
Add this to your MY_Controller and see what is returned
var_dump($this->ion_auth->logged_in());exit;
I have various static pages and I dont want to create independent controller for them all.
Say I want to create a controller named page and have all of the static pages as functions.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Page extends Frontend_Controller {
public function index()
{
}
public function store(){
$this->load->view('public/store');
}
public function contact(){
$this->load->view('public/contact');
}
public function about(){
$this->load->view('public/about');
}
}
Is there anyway to modify the URI routes so that I wouldnt have to type mydomain.com/page/contact but I could type mydomain.com/contact to view the page
In your application/config/routes.php
You can add as:
$route['contact'] = "page/contacts";
$route['store'] = "page/store";
...
In my application/config/routes.php I use the following regex to do that. Most sites I work on are relatively small so I list all of the controllers in the following snippet (because it's easier than listing all of the static pages)
$route['^(?!controller1|controller2).*'] = "page/$1";
The logic basically says that If the path does not start with controller1 or controller2 then use page as the controller for urls like http://domain.tld/page
For not having to create 10+ static routes for all my static pages it keeps it pretty simple.
Hi I'd like to add a public variable ($this->data[]) to all instances of CI_Controllers, that way I can store some base rules for outputting a page (css/js, etc) then have each controller append to this array to add its own requirements (more css/js). I have a core library with custom view functions that take those arrays and inject them into the head tag of the page template.
The options I've thought of;
Edit CI_Controller and add it there... guessing that's a bad idea.
Create a shell controller that extends CI_Controller, add the var to that, then have every other controller extend the shell controller.
Any other clever ways?
I've only been using CI for about a month and I've tried reading through the docs but I can't find any built in ways to do something like this? Has anyone encountered this before and if so how did you solve it?
Thanks!
[edit] Using PHP 5.3.x [/edit]
http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html
You can extend CI_Controller to have the functionality you want.
application/core/MY_Controller.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function my_function()
{
return "Cool return from my_function";
}
}
controllers/welcome.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends MY_Controller {
public function index()
{
echo $this->my_function();//echo's "Cool return from my_function"
}
}
You simply define the functionality you want in MY_Controller. Then in your controllers, use extends MY_Controller instead of extends CI_Controller and you can call the functions anywhere inside those controllers.
I think what you want can be easily achieved using traits. Check here: PHP: Traits
More specifically -> Example #11 Defining Properties.
The only limitation is it's PHP 5.4+
You can create your own a base controller file to inherit basic page load methods from in \application\core\MY_Controller.php as such:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
}
protected function loadPage($path, $data = array())
{
$this->load->view('common/head'); // Assuming you will use a folder for page parts
$this->load->view($path, $data);
$this->load->view('common/foot');
}
}
Afterwards in your page controller you can call upon the same methods much more easily:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class User extends MY_Controller {
public function index()
{
$data = array(); // get data from model
$this->loadPage('user/view', $data); // Assuming you will use folders for sets of views
}
public function edit()
{
$data = array(); // get data from model
$this->loadPage('user/edit', $data);
}
}