How deploy in heroku codeigniter? - php

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

Related

Change default controller directory of Codeigniter

I want to have two folders where save codeigniter's controllers:
/application/controllers
/application/buckets
i'm a order paranoic person and i want to separate two types of my controllers.
In bucket folders the structure app was this:
/application/buckets/example/index.php
/application/buckets/example2/index.php
/application/buckets/example3/index.php
¿Maybe extending the router class?
A working example:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
Extended the core Router class to allow for sub-sub-folders in the controllers directory.
*/
class App_Router extends CI_Router {
function __construct()
{
parent::__construct();
}
function _validate_request($segments)
{
if (count($segments) == 0)
{
return $segments;
}
if (file_exists(APPPATH.'buckets/'.$segments[0].'/index.php'))
{
$this->set_directory(APPPATH.'buckets/'.$segments[0]);
$this->set_class(ucfirst($segments[0]));
$this->set_method(isset($segments[1]) ? $segments[1] : 'index');
return $segments;
}
}
}
You can use Hierarchical MVC(HMVC) with Codeigniter to accomplish this.
For reference, see Modular Extensions - HMVC
You may want to look into parent-child controller ...one extending another. To be more clear you can make as many controller you want.
I Agreed with #Brian Gottier : "what does changing their location do?"
You can perform anything if you have core functionalities in your hands.
You can play around with hooks (CodeIgniter's Hooks feature provides a means to tap into and modify the inner workings of the framework without hacking the core files. When CodeIgniter runs it follows a specific execution process, diagramed in the Application Flow page.)
Create "Base"/"Admin"/"Public"/"XYZ" Controllers in
application/core/MY_Controller.php
and keep rest of your controllers in same application/controller folder
MY_Controller is a basic core library extension. Whenever you create a class with the MY_ prefix the CodeIgniter Loader class will load this after loading the core library.
All we have done here is create a base class that all of our Controllers and "controller types" will inherit. Anything we put in here and assign to $this will be available to anything that extends this class.
Base Controllers are a nice simple way to give you global data, logic and shared code which can be specific to a certain part of your site. They can do all sorts of crazy stuff which I will leave for you to think about.
I Hope this help.

Codeigniter 3 - Make URLs case insensitive

With CI3 released controllers must now be Ucword-style (for whatever reason). No problem changing these, but upgrading any site now leads to 404s wherever it's applicable (which is pretty much everywhere).
Is there a way to make it so the old URLs still work (in addition)? Ie I have a controller 'Admin.php" the index() fn of which used to be called
http://example.com/admin
now it must be called
http://example.com/Admin
Is there a way to have both work (on CentOS). Maybe via Apache rewrite and/or config?
You can set your routes in :
application/config/routes.php
$route['admin'] = 'admin/index';
You can set your Controller as following code in controller.
application/controllers/Admin.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Admin extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model(array('admin_model'));
}
public function index()
{
$this->template->view('admin/index');
}
}
?>
If case insensitive routes are required, do below changes to URI.php
Location of File: system/core/URI.php
Find $this->_parse_request_uri() and replace it with strtolower($this->_parse_request_uri())

Codeigniter and Mongodb library - do I need a model?

I am trying my hand out with mongodb as a backend to my codeigniter application.
I found and followed this example: http://www.surfinme.com/codeigniter-mongodb/
So I have the following structure:
+application\
libraries\
Mongo_db.php
config\
mongodb.php
controllers\
api.php
This is what my api.php file looks like so far:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Api extends CI_Controller {
public function __construct()
{
parent::__construct();
//loading the mongodb library
$this->load->library('mongo_db');
}
public function index()
{
$data['main_content'] = "dashboard";
$this->load->view('includes/template',$data);
}
public function list_available()
{
//connect to mongodb collection (i.e., table) named as ‘surfinme_index’
$collection = $this->mongo_db->db->selectCollection('testcollection');
$result=$collection->find();
foreach($result as $data)
{
var_dump($data);
}
}
}
And the code seems to be working. It gets my data out of the database and dumps it to my browser. But this "feels" wrong becuase I don't have a model.
Or should I be viewing the library as my model layer?
I could just as easily create a file in the models folder called api_model and make the calls to the library from there.
But is that overkill?
Any comments would be appreciated.
Thanks.
Depends on how tightly you want to adhere to MVC principals.
By creating a model for database operations you get several potential benefits. First, it provides a source that can be reused by multiple controllers. For example, there may be more than one controller that uses selectCollection. Second, if for some reason you decide to change databases you only have to modify the Models as opposed to every controller that makes calls to mongo_db.

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

Codeigniter - load a model in my library

I'm about to write my own library in codeigniter to check if the logged in user is an admin or not. To do that I need to compare a value with the value of the typeAccount in the DB.
Because I'm still learning the MVC pattern I had a question about this before starting to write my library.
Can I load a model in my library? Or should I communicate directly to my DB in my library? Or is there a better way to approach this?
Yes, you can load your model to into your library, simply add CI.
class Validator
{
private $CI = null;
function __construct()
{
$this->CI =& get_instance();
}
public function validate_merchantaccount_status($param)
{
//Code Here
$this->CI->load->model('merchantaccount_model');
$res_merchant = $this->CI->merchantaccount_model->get_list($param);
}
}
Dont forget to make your model.
Since the library is some kind of logic, you can see it as a piece of the Controller.
The Controller usually just loads a Model to use it.
So yes just load the Model from CodeIgniter instead of connecting to the database yourself.
Makes your code more DRY too.
Make you models
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class My_model extends CI_Model
{
/// Here the code
}
And then in controller use
$this->load->model('my_model');
Ant i think that is best approach :)

Categories