I have PHPExcel, and I want to add it to CodeIgniter.
Where is the best to copy the files from all CodeIgniter folders, and how do I call it in my controllers/models for regular usage?
EDIT:
It's a whole folder that I need to move, and call the main file: PHPExcel\IOFactory.php
You may add PHPExcel to library in Codeigniter, E.g:
application > libraries > PHPExcel.php
Let say in PHPExcel.php library file:
<?php
if (!defined('BASEPATH')):
exit('No direct script access allowed');
endif;
class PHPExcel
{
public function __construct()
{
//
}
public function some_function()
{
return 'some_function';
}
}
To call PHPExcel library, in your Controller. Let say I named it as
My_controller.php:
<?php
if (!defined('BASEPATH')):
exit('No direct script access allowed');
endif;
class My_controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
//Call PHPExcel class
$this->load->library('PHPExcel');
}
public function index()
{
echo $this->PHPExcel->some_function();
}
}
I followed this tutorial: https://arjunphp.com/how-to-use-phpexcel-with-codeigniter/
Got my answer !
Related
I want to set my session variable in every function of the class. If the session is not set in any of the function , then it should be redirected to home page
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class App_config extends CI_Controller {
public function masteradmin()
{
if($this->session->userdata('name')){
$data = $this->session->all_userdata();
$this->load->view('user/masteradmin',array('data'=>$data));
}
else
{
redirect('/', 'refresh');
}
}
public function reseller()
{
}
}
Use constructor for that. In OOP constructor is the first method of any class which is called. So, what you can do is, create a constructor and check for the session variable. If the variable exists, do nothing, it will automatically call the function. If the session variable does not exist, redirect the user to the home page.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class App_config extends CI_Controller {
function __construct(){
parent::__construct();
if(!$this->session->userdata('name')){
redirect('/home');
}
}
public function masteradmin(){
$data = $this->session->all_userdata();
$this->load->view('user/masteradmin',array('data'=>$data));
}
public function reseller(){
}
}
?>
create super class in core folder like MY_Controller.php
and there you can define session and then call that super class in every controller extends. for example:
class App_config extends MY_Controller {
}
I have old urls like http://example.com/products.php?s_productid=231 and now in my proyect whit url would be http://example.com/products/ver/231
How can I make a rule in routes.php to redirect?
Thanks
you don't need to do any change in routes.php, all you need is to create a Controller "Product", with one Method "ver", which receives 231 (it's id I guess) as a parameter, like this:
defined('BASEPATH') OR exit('No direct script access allowed');
class Product extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function ver($id) {
// do your things
}
I hope it helps.
You may need to set the routes in config/routes.php
$route['product/ver/(:num)'] = 'product/ver/$1';
http://example.com/index.php/products/ver/231
Then the controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Product extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function ver($id) {
/*
echo $id;
*/
}
I have one controller abc_con with view abc_ view.
There is second controller cart_con with cart_view.
And I have third controller xyz_con with view xyz_view.php
The flow of my website is like when I click on abc_view then values go to abc_con and in abc_con there is function cartload(). This function load cart_view view. When cart_view submit all value go to xyz_con controller. In xyz_con there is function loadxyz(). This function load the xyz_view.
In abc_view there is an input with name pin code. This input value I can easy fetch in abc_con
I just wanted to transfer this $data t
Directly to xyz_view without storing in database.
You have to extend those controllers together
like this
1).in oneController.php
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
require APPPATH . '/controllers/twoController.php';
class oneController extends BaseController
{
$this->abc();
}
2).in twoController.php
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
require APPPATH . '/controllers/threeController.php';//parent class is threeController now you can access to function abc using inheriting like this
class twoController extends threeController
{
$this->abc();
}
}
3).in threeController.php
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
class threeController extends CI_Controller
{
function abc()
{
your code
}
}//end of 3rd controller
How to code 2 application on codeigniter.
Example I have 2 application : Facebook and twitter use Codeigniter.
On home I can choose go to facebook or twitter.
This is a easy demo.
In controllers folder:
You create a controller: Home_controller
class Home_controller extends CI_Controller {
public function index()
{
echo "<a href ='index.php/facebook_controller'>Facebook</a><br>";
echo "<a href ='index.php/twitter_controller'>Twitter</a>";
}
}
Then you create facebook_controller and twitter_controller like this:
Facebook_controller:
class Facebook_controller extends CI_Controller {
public function index()
{
echo "This is facebook_controller";
}
}
Twitter_controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Twitter_controller extends CI_Controller {
public function index()
{
echo "This is twitter_controller";
}
}
Hope this help.
I have a problem in login application in codeigniter , the problem is like this:
I create MY_Controller, like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
public function __construct(){
parent::__construct();
}
}
Then I extended it on my library that i created, this is my "Petugas_Controller" library:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Petugas_Controller extends MY_Controller {
public function __construct(){
parent::__construct();
$this->load->model('petugas_m');
} // end contructor.
public function check(){
if ($this->uri->uri_string() !== 'index' && ! $this->petugas_m->is_logged_in() == TRUE){
redirect('admin/petugas/index');
}
}
}
I'm using that library to be used in my "Petugas" controller, like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Petugas extends MY_Controller {
public function __construct(){
parent::__construct();
$this->load->library('petugas_controller');
$CI = &get_instance();
$CI->check();
}
public function index(){
echo "You have to login";
}
public function dashboard(){
echo "Welcome to dashboard";
}
}
In "Petugas_Controller" library, I loaded model that is called "Petugas_m", here is the model :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Petugas_m extends CI_Model {
public function __construct(){
parent::__construct();
}
public function is_logged_in(){
return FALSE;
}
}
That's all what i was trying, and my problem is I want to check if username is logged or not, by doing $this->check() in "Petugas_library" that loads "Petugas_m" model, i could see an dashboard echo if the "check" method in "Petugas_m" is set to TRUE but it's always redirect me a loop if it FALSE.
I was searching for tutorials and still have this crazy problem :( ,
Thank in advance, and sorry for my bad english.
As long as your petugas_controller library is located within the libraries folder, you can access the check method via
$this->petugas_controller->check();
Also try changing the if statement in your check to something more like this
if ($this->uri->uri_string() !== 'index' && $this->petugas_m->is_logged_in() !== TRUE){
lol, I've just realized my mistake here, I've just tried to make another controller, it's called login, then in my "petugas_controller" library I redirect the user if not login to this "login" controller, it's totally worked ...
Youy guys are inspiring me so much,Thanks :)