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.
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();
}
}
I am developing a web app using codeigniter framework. My application has a fixed header and footer.I want to middle section (ie the body) of my application to load when the user goes to various pages which are available to him and make the header and footer constant.
(hackerrank.com ... I was talking about a website similar to this one ... after login into this website ... the header and the sidebar of this remains constant and they load the remaining page ... how can we implement it using CI framework)
Is there any way through which I can achieve this?
I am performing the following actions which is making me to load the complete website(Its like reloading the entire page :/)
As You can see the following code is present in my template.php
<?php
$this->load->view("templates/header.php");
$this->load->view($main_body);
$this->load->view("templates/footer.php");
?>
and I Controller I write the following piece of code usually
public function load_page(){
$data['main_body'] = 'dashboard_pages/dashboard_view';
$this->load->view('template.php',$data);
}
You were pretty close to the solution: Load the header and the footer as they are, variables:
<?php
$this->load->view($header);
$this->load->view($main_body);
$this->load->view($footer);
?>
The tricky thing, is that you're always writing the method function load_page() in every controller you write, and it would be better having a MY_controller, a class previous to your controller class in which you'll write which footer you're referring:
Check for writing a MY_Controller here: http://ellislab.com/codeigniter/user-guide/general/core_classes.html
Then, write your MY_Controller:
class MY_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
}
public function load_page( $page )
{
$data['main_body'] = $page;
// Logic for your header or footer deppending on logging or whatever...
if ( 1 ==1 ) {
$data['header'] = "templates/header.php";
$data['footer'] = "templates/footer.php";
}
$this->load->view('template.php',$data);
}
}
and you'll have to make sure your controllers extends MY_Controller class, and add a load_page method to whom you'll pass the argument:
class Custom_page extends MY_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$this->load_page( 'dashboard_pages/dashboard_view' );
}
}
I think with that you'll have exactly what you look for, that way you only have to write in one place the logic for the header and footer: In MY_Controller, and just use it in any part.
Currently my URL is domain.de/site/home, domain.de/site/about.
But i want that my URL ist domain.de/home, domain.de/about etc.
I already have a really simple Controller. In this Controller i`ve a function for each Site:
<?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");
}
}
Open application/config/route.php and add following route path
$route['home'] = "site/home";
$route['about'] = "site/about";
You can define the association between a URL pattern and a controller/action in your config/routes.php file. Read the docs here and you will understand how it can be done.
By default, your routes will be {domain}/{controller_name}/{action_name} unless you override that in routes.php where you can assign to your controller/action a different URL pattern.
So probably in your case you would need something along the lines of:
$route['home'] = 'site/index';
$route['about'] = 'site/about';
Even better you might want to set your site controller as default controller:
$route['default_controller'] = "site";
Then all requests starting from the base url will get directed to actions in your site controller. That is {domain}/{action_name} will be directed to site/action_name automatically.
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...
Ok, so in my base controller (page.php) I have the following code which works fine:
$this->load->library('Siteclass');
$mysite = new site_model();
The siteclass library references a model named site_model and instantiates based on data received from that model. All is good.
Now I want to load another library so that I can instantiate another object as well. So I add this to page.php:
$this->load->library('Memberclass');
$mysite = new member_model();
But now I get the following error:
Message: Undefined property: Memberclass::$site_model
Filename: libraries/Loader.php
Line Number: 1035
From what I can tell, it seems that the loader class, when being applied to the Memberclass, is somehow still referencing the site_model instead of the member_model. I've checked my code and I am definitely calling the correct files.
Here's what Siteclass.php looks like:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Siteclass extends Controller {
function __construct() {
parent::Controller();
$this->load->model('Site_model');
$data = $this->Site_model->load_site_data();
// etc etc
and here's what Memberclass.php looks like:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Memberclass extends Controller {
function __construct() {
parent::Controller();
$this->load->model('Member_model');
$data = $this->Member_model->load_member_data();
// etc etc
Thanks in advance for any help!
Gary
I think you're confused about how MVC works in CodeIgniter. Why are you using the loader class to create a controller? Why are you creating a stand-alone instance of your model outside of your controller class?
In CodeIgniter, your URLs represent paths to your controllers' methods. That means that your "base controller" should automatically be instantiated if you go to:
www.example.com/memberclass
Or perhaps more to the point, if you have a link like this:
www.example.com/page
You should have a file in your /application/controllers directory called page.php which looks like this:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Page extends Controller {
function __construct() {
parent::Controller();
// etc etc
Furthermore, unless you're loading data from your model to be used every single time you call this controller, you'll want to put your model calls inside a non-constructor method of this class. Something like:
class Page extends Controller {
function __construct() {
parent::Controller();
}
function index() {
$this->load->model('Member_model');
$data = $this->Member_model->load_member_data();
$this->load->view('myview', array('data'=>$data));
}
}
So again...not entirely sure what context you're doing this all in, but it seems like you're not standing firmly within the framework. There's basically no reason you should be using the loader class to load controllers, and furthermore there's no reason you should be creating stand-alone instances of model classes using PHP's new keyword.