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';
Related
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.
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
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.
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;
}
}