understanding the basic codeIgniter - php

<?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

Related

How deploy in heroku codeigniter?

I am trying to deploy my application in heroku with codeigniter, I have a series of errors that I have been solving, but I would like to know the correct way to configure the framework codeigniter to be able to deploy it in heroku, the database is configured and I have no problems with your connection.
At this point I have problems with loading the helper.
Problem 1:
My helper located in the corresponding folder "helpers"
and its name is cargarConsultas_helper:
<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');
function getAreas()
{
$ci =& get_instance();
$query= $ci->db->get('areas');
return $query->result();
}
?>
my controller in which I charge the helper:
public function __construct()
{
parent::__construct();
$this->load->helper(array('url', 'cargarConsulta'));
}
the error that throws me is:
error
I thank you for your help.
Try to rename your helper where the first letter only is upper case same applies for controllers and models and libraries etc
Filename Cargarconsultas_helper:
$this->load->helper(array('url', 'cargarconsulta'));
Class and filenaming guide
https://www.codeigniter.com/user_guide/general/styleguide.html#file-naming
https://www.codeigniter.com/user_guide/general/helpers.html
Replace $this->loaa->helper('cargarConsultas') error came because in your filename cargarConsultas in that C is capital and in helper you wrote c small so it unable to load that file

How do I make codeigniter to load a HTML project?

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?

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

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

Better file naming conventions or structure

I create folders and then add files e.g.
folder: orders
files : view.php, add_update.php, invoice.php
Uri becomes: orders/view, orders/add_update, orders/invoice
another example
folder: user
files : view.php (view profile), add_update.php (add, update users)
Uri : users/view, users/add_update
and many more folders and files having similar structure. It works fine but in the editor (netbeans) I see the the name of the file only in the tab so that if I have opened order/view.php, users/view.php, files/view.php I see view.php, view.php, view.php ... I can certainly hover over and see the full path but I was wondering if you guys follow a better file naming pattern. I sometime make mistakes when uploading orders/view.php or users/view.php cos the files structure look so similar. What would you suggest?
PS: These are controllers not the CI view files. One of the reasons I have this structure is to have meaningful uri. I just tagged in CI but it doesn't matter if I am using CI or not.
Why the need for folders? Why not simply have a class of Orderswith methods view, update, etc?
Barebones example:
<?php if (! defined('BASEPATH')) exit('No direct script access');
class Orders extends CI_Controller {
function __construct() {
parent::__construct();
}
/* URL = example.com/orders */
function index()
{
/* default action */
}
/* URL = example.com/orders/view */
function view()
{
/* view method code */
}
/* etc */
}
you can rename your files like order_view.php, order_update.php etc and similarly user_view.php, user_update.php... it will differentiate the orders and users in your IDE
I would suggest working with namespaces, and give the views a more meaningful name.
This is how my Netbeans looks:
And I have assigned namespace \Ganymedes\Views to all of them. So I have meaningful names, and still everything in the same namespace.

Categories