Access base_url() Or site_url() in CodeIgniter's custom config file - codeigniter

I want to accessbase_url() method so that I can get my application url in custom config file.
I don't want to write $config['base_url'] = 'some url'; in custom config file.

Your question unclear but give it ago. To enable base_url load the url helper
$autoload['helper'] = array('url');
$autoload['config'] = array('custom'); // application > config / custom.php
The create the custom config file
<?php
// Testing
echo config_item('base_url');
$config['test'] = '111';
Or on controller load in the __construct area
Check filenames and classes starts with first letter only upper case
<?php
class Welcome extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('url');
}
}
Make sure you have set your base url in CodeIgniter 3 and above
versions it is recommended.
$config['base_url'] = 'http://localhost/project/';

To use base_url(), you must first have the URL Helper loaded. This can be done in application/config/autoload.php, then:
$autoload['helper'] = array('url');
Or manually:
$this->load->helper('url');
To print returned value:
echo base_url();

You can use $this->config array that contains all settings loaded by default.
For example: you can create sample.php config file and put next code into
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| Sample
| -------------------------------------------------------------------------
|
|
*/
//here you can see how is used base_url value from config.php
$config['r'] = $this->config['base_url'];
In controller you are calling it as any other config item:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller
{
public function index()
{
var_dump($this->config->item('r'));
}
}
Autoload sample.php in config section of autoload.php file or manage loading as appropriate for your application.

Related

How to load a library from composer?

I load a composer library suited for CodeIgniter called SteeveDroz\Asset, that I can access without problem with $asset = new SteeveDroz\Asset.
I would like to be able to load it with CodeIgniter $this->load->library('SteeveDroz\Asset'), but I get the error message
Unable to load requested class: SteeveDroz\Asset
Is it possible to achieve what I want? If yes, how?
As mentionned Alex in his comment, it is required to make an adapter library. I created an all purpose class for that:
application/libraries/ComposerAdapter.php
class ComposerAdapter
{
private $object;
public function __construct($object)
{
$this->object = $object;
}
public function __call($method, $args)
{
return call_user_func_array([$this->object, $method], $args);
}
}
application/libraries/Asset.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require('ComposerAdapter.php');
class Asset extends ComposerAdapter
{
public function __construct()
{
parent::__construct(new SteeveDroz\Asset());
}
}
application/config/autoload.php
// ...
$autoload['libraries'] = array('asset');
// ...
if you are using CodeIgniter 3 you can modify application/config/config.php and set
$config['composer_autoload'] = TRUE
or
$config['composer_autoload'] = FCPATH .'vendor/autoload.php';
this will automatically load all your composer dependencies.

How to redirect old php routes in codeigniter

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;
*/
}

Codeigniter routing works in local but not in server (404)

The routing I'm using is working in local but not in server.
I have this controller which only loads a view :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class CgvController extends CI_Controller{
public function index(){
$this->load->view('cgv');
}
}
I can access it using example.com/index.php/CgvController/index (in local I can access it using only /CgvController/index), and I would like that when i'm writing example.com/cgv.html I get the same page.
My routes file is :
$route['default_controller'] = 'IndexController';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['cgv\.html'] = 'CgvController/index';

CodeIgniter + PHPExcel: What is best way to call this library to CodeIgniter?

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 !

Codeigniter - uri segment

i need to create an url like this: www.example.com/index.php/scheda/name-surname-id.html
so i create the Scheda controller, this is the code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Scheda extends CI_Controller{
function __construct(){
parent::__construct();
}
function index(){
$name_scheda = $this->uri->segment(2);
//i need only the id for the search into db
$id = substr($name_scheda, strripos($name_scheda,'-')+1, strlen($name_scheda));
echo "name:".$id;
}
}
but when i write the url in the address bar i get an 404 error...can someone help me to understand why?
Your url:
www.example.com/index.php/scheda/name-surname-id.html
Should be:
www.example.com/index.php/scheda/index/name-surname-id.html
index() is the default method, but the index segment can only be missing from the URL if there are no arguments, otherwise Codeigniter will think you are trying to call the method name-surname-id.html().
You can use routes.php or _remap() to clean up the URL and remove the index segment.
// routes.php
$routes['scheda/(:any)'] = 'scheda/index/$1';
OR:
class Scheda extends CI_Controller{
function _remap($method, $args) {
$name_scheda = $method;
$id = substr($name_scheda, strripos($name_scheda,'-')+1, strlen($name_scheda));
echo "name:".$id;
}
}

Resources