How do I make codeigniter to load a HTML project? - php

I have a project which is this one:
Its just a html webpage with the features of letting the user modify the product the way they want, it currently does not have any backend.
I have done this:
In my app/controllers I have a made one controller named:desing
it has this
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class desing extends CI_Controller {
public function index()
{
$this->load->add_package_path(APPPATH.'third_party/desing_tool', FALSE);
$this->load->view('index');
}
}
I have the third party project in the folder third_party and my routing settings are this:
$route['desing'] = 'desing';
but it doesnt load, I want to add a shopping cart, I will make a cms but I cant make code igniter load this.
Any Ideas?

Related

Separating Controllers for each module in Code Igniter

P.S This question may already be existing but the answer didn't satisfied what I was trying to figure out. I'm using code igniter for the first time in my new project.
So I have this only one controller which is main.php and populated with a lot of public function. Now everytime I go to main.php the url is looks like cmms/main and everytime I'm going for its subclass it goes cmms/main/asset.
Now the subclass asset has many functions which are in main.php. What I want is to make separate controllers for each module. so i have cmms/main as main.php & cmms/asset as asset.php instead of making it under cmms/main/asset. Would this be possible? or I should just leave it alone and continue putting all codes in the main.php controller?
My default route is the main controller.
$route['default_controller'] = 'main';
You have two ways to do that.
Keep one controller but URL is different. (Method 01)
Keep new controller for each new function. (Method 02)
(these topics describe below)
Method 01
go to - config/routes.php, and add new routes like this
$route['cmms/asset'] = 'cmms/main/asset';
$route['cmms/contact'] = 'cmms/main/contact';
So in view you should call anchor tags like this
Assets
Contact Us
Method 02
Create new controller for each new methods.
File name - main.php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Main extends CI_Controller {
}
File name - asset.php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Asset extends CI_Controller {
}
File name - contact.php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Contact extends CI_Controller {
}
So in view you should call anchor tags like this
Assets
Contact Us
you can create Modules for your class in following ways,
By creating separate class under your application controllers directory eg: main.php, assets.php here your URL will be cmms/main or cmms/asset
Creating sub folders under your application controllers directory and create controller classes under it main/main.php, asset/asset.php here URL will be cmms/main/main, cmms/asset/asset, if you want just cmms/main or cmms/asset in URL than you need to create routes in route.php file.
This way you can make codeigniter Modular in this way every module will have separate controller, model and view even libraries in its own module directory and URL will be same as cmms/main or cmms/asset for each controller. you need to install wiredesignz's HMVC extension here is URL you can download https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/overview
important please read documentation for HMVC extension to use it.
it seems that you are having trouble to write long url each and every time....
So,better way is to use routes for ease.
For Ex: $route['login'] = "main/login";
Now if you use login directly in url it will call main class and it's login method
using routes you can prevent exposing your controller name and also for shorten URl

CodeIgniter MVC - Is it a good practice to have Models serve views?

I'm developing a news website using CodeIgniter 3. Almost every page contains a Notice Board content of which is same on all pages. I have created a view for this section named 'noticeboard.php'. And different pages are controlled by different Controllers. i.e. Home, Post, Gallery, Poll, etc. The problem using controller here is I need to perform two operations on every controller to generate the noticeboard HTML.
Controller looks like:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class SomeController extends CI_Controller {
/* .. */
public function index() {
/* .. */
$noticeboard['notice'] = $this->noticeboard_model->getNotice(); // Gets Notice's HTML
$data['noticeboard'] = $this->load->view( 'sidebar/noticeboard', $noticeboard, TRUE );
/* .. */
/* $data is served to main page template */
}
/* .. */
}
And views/sidebar/noticeboard.php looks like:
<div class="noticeboard">
<h3>Notice</h3>
<?php echo $notice; ?>
</div>
I could solve this code redundancy by simply having a getNoticeboardHTML() on Noticeboard model and having the model fetch the view and return HTML. But is it a good practice?
Better you create a custom library and put getNoticeboardHTML() in that class. After that call this wherever you need. Do not dirty your model with HTML.
Create Custom Library
Theoretically speaking, No. You are not supposed to do this. Its a violation of MVC. You could use a library class to avoid the code duplication.

Codeigniter - Extending my own controllers

I'd like to know if it's possible to extend my own controllers. I've been working on web-based applications for a while and I'm now starting to find each customer that wishes to use the application has different requirements of how it should work. My thoughts are that if I generate a base structure and then extend the controllers to override any of the functions that the customers require to work differently.
First of all, could you tell me if I'm on the correct track, and secondly, how do I go about extending my own controllers (if I can)? I've tried the usual:
Class Reports2 extends Reports { }
This doesn't work but I'm guessing it has something to do with the location of the file I'm trying to extend. My file structure is as follows:
Application
--->controllers
-------->control_panel
------------>reports.php
If I am not mistaken then you should be able to easily do this:
reports2.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once(APPPATH.'controllers/control_panel/reports.php');
Class Reports2 extends Reports {
public function __construct(){
parent::_construct();
}
public function index(){
}
}

how to integrate whole html template in codeigniter?

I also done a single webpage into the codeigniter.
But performing "href" it doesn't works.
Give a solution for how to include whole site into the codeigniter.
I think may be i need to create controller(into the directory) files for each and every view(into the directory) files.
Note : My whole templates are in the "view/pages" folder.
i already created "pages.php" in controller folder.
After changing route.php it only works on index.php. not for all other pages when perform links from index page.
My pages.php code is below
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Pages extends CI_Controller
{
function view()
{
$this->load->view('templates/header.php');
$this->load->view('pages/index');
//$this->load->view('pages/login');
$this->load->view('templates/footer.php');
}
}
?>
I dont understand you. Do you mean something like this?
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Pages extends CI_Controller
{
function index()
{
$this->load->view('templates/header.php');
$this->load->view('pages/index');
//$this->load->view('pages/login');
$this->load->view('templates/footer.php');
}
function aboutUs()
{
$this->load->view('templates/header.php');
$this->load->view('pages/aboutUs');
$this->load->view('templates/footer.php');
}
}
?>
In this way you reach your index page through www.yourserver.ext/pages/ (or www.yourserver.ext/pages/index) and, for example, your about us page through www.yourserver.ext/pages/aboutUs
Also you can set a param and load the page depending on it

understanding the basic codeIgniter

<?php
if (!defined('BASEPATH', ''))
exit('no direct script access allowed');
class Hello extends CI_Controller {
public function index() {
echo "this is my index function";
}
public function one() {
$this->load->view('one');
}
}
?>
I am the new user of codeIgniter.First i download code igniter software,then place it to the xampp/htdocs.Then i create a simple codeIgniter code which are show in the above.I don't understand where this code is to be save.I save this code in the Codeignitor/appilication.but it give me the below answer:
no direct script access allowed
What are the fault.please help me anyone.
Have you read the User Guide of Codeigniter? This is your Controller code and it should be placed inside appilication/controllers folder by name hello.php. See this.
Before starting of any framework it would be good if you understanding directory structure, and what is the role of files.
Where should keep HTML, CSS, JS and logic and model part.
As you creating controller then it should save under
application/controllers/ControllerName.php
Here is the guideline for controller
Also refer tutorials
http://tutorialcodeigniter.com
http://codesamplez.com/development/codeigniter-basic-tutorial
Open config.php within CodeIgniter\system\application\config.
Change base site url http://localhost/foldername

Categories