Codeigniter Change URL method name - php

I am new in CI.
I want to change function name in addressbar url with add_car to addcar.
Actually my url is created as below
http://localhost/projectName/controller/add_car
But I want following in URL
http://localhost/projectName/controller/addcar
Is it possible? Please help me.
[Note] : My actual method name is add_car.

You can do it by two methods
Method 01
Edit - config/routes.php
$route['controller/addcar'] = 'controller/add_car';
$route['controller/deletecar'] = 'controller/delete_car';
output - www.exapmle.com/controller/addcar
Method 02
change your controller function name as you like.
public function addcar($value='')
{
# code...
}
public function deletecar($value='')
{
# code...
}
Output -www.exapmle.com/controller/addcar
Further Knowledge
If you use $route['addcar'] = 'controller/add_car'; URL looks like
www.exapmle.com/addcar

Change add_car function to addcar in your controller
function add_car(){
//...
}
To
function addcar(){
^
//...
}
Or in routes.php
$route['controller/add_car'] = "controller/addcar";

$route['controller/([a-z]+)_([a-z]+)'] = "controller/$1$2";
Above example will route every requested action containing '_' between two strings to action/method without the '_'.
More about Code Igniter regular expression routes:
https://ellislab.com/codeigniter/user-guide/general/routing.html

You can use this on your route:
$route['addcar'] = 'Add_car/index';
$route['addcar/(:any)'] = 'Add_car/car_lookup/$1';
and your controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Add_car extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function car_lookup($method = NULL)
{
if (method_exists($this, $method))
{
$this->$method();
}
else
{
$this->index(); // call default index
}
}
public function index()
{
echo "index";
}
public function method_a()
{
echo "aaaaa";
}
public function method_b()
{
echo "bbbbb";
}
}

Related

How to redirect from view to controller in codeigniter?

In my header view I wrote this code:
<?php
if($this->session->userdata('logged_in')) {
$query = $this->db->get_where('instructors', array('id' => $this->session->userdata('id')));
$insdatacheck = $query->row_array();
if($insdatacheck['name'] == '') {
redirect(base_url().'user/continueregistration');
} else { ?>
<script type="text/javascript">alert('test');</script>
<?php
}
}
?>
But it does not redirect to the following page. However, if I write this in the controller, it works properly. I wrote it in header view because I want to check it in every page where enters the user. How can I improve it and write in a proper way? Thanks in advance
I think instead of your header you should put your check inside your controller constructor.
class Test extends CI_Controller {
function __construct() {
parent::__construct();
// if not logged-in redirect to login page
if ($this->session->userdata('logged_in') == false) {
redirect('login'); // where you want to redirect
}
}
}
Another option is to create a base controller. Place the function in the base controller and then inherit from this.
To achieve this in CodeIgniter, create a file called MY_Controller.php in the libraries folder of your application.
class MY_Controller extends Controller
{
public function __construct()
{
parent::__construct();
}
public function is_logged_in()
{
$user = $this->session->userdata('user_data');
return isset($user);
}
}
Then make your controller inherit from this base controller.
class X extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function do_something()
{
if ($this->is_logged_in())
{
// User is logged in. Do something.
}
}
}
First create session in the controller only,
Then we access session in any page,
$this->load->library('session');
$user=$this->session->userdata('logged_in');
if (!isset($user)) {
redirect(base_url().'user/continueregistration');
}
else {
<script type="text/javascript">alert('test');</script>
}

if codeigniter enable query string in controller but could not redirect on my controller method? why

If codeigniter enable query string in controller but could not redirect on my controller method? why?
codeigniter config.php file providing this feature you have simply to activate rewrite this query
$config['enable_query_strings'] = TRUE;
At the same time our controller could not to redirect the controller methods
For example I am used this version CodeIgniter v3.0.3 (Current version).
/home/test/Pictures/SDScreenshot from 2015-11-07
<?php
defined ('BASEPATH') or exit('No direct script access allow');
class Route extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
function index()
{
$this->setting();
}
public function setting()
{
$object=&get_instance();
$this->path();
}
public function path()
{
$this->load->view('route_view');
}
function log()
{
$base=site_url().'/Route/auto_range';
redirect($base);
}
function auto_range()
{
echo 'simple auto_range is alreafy modified';
}
}

Best Location to write the Session code to check if Session Exists

Using : PHP MVC CI
I created a Controller class with the name of My_Controller in Core Folder. In this class, It is being checked if the session exists or not. This controller is being extended in all controller classes. Here is the code
<?php
class My_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->library('session')
if(empty($this->session->userdata('userName')) {
header('Location: '."Login Url");
}
}
}
?>
Question: Is there any better location to write the Session code in MVC Architecture ??
You don't have to use session_start();
In Controller
class My_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
}
public function index($value)
{
$session = $this->model_name->check_session();
if($session==true){
#valid code
}
else{
redirect('controller/method');
}
}
}
In Model
public function log_in()
{
$log = $this->session->all_userdata();
if (isset($log['userName'])) {
return true;
} else {
return false;
}
}
you need to create the hook in CI and check the session over there.
Hook will call in every request so you can write permission role over there.
Here is more detail how you can write the hook.
http://www.codeigniter.com/user_guide/general/hooks.html
create a file name auth_hook.php in hook folder and write this code
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Auth_hook extends CI_Controller {
private $CI;
public function __construct() {
$this->CI = & get_instance();
}
public function is_authorized() {
$uri = $this->CI->uri->segment(1);
if (strcmp($uri, 'user') && $uri != '') {
if ($this->CI->session->userdata('logged_in')) {
return true;
} else {
redirect(site_url('user'));
}
return true;
}
return true;
}
}

codeigniter route does not route as expected

in php word "list" is reserved so i had to use "listby" and create route.
According to CI User Guide I have created a route as follows:
$route['list'] = "listby";
It's routing perfectly the index function with url like "http://myserver.com/list" but does not route other functions i.e.. "http://myserver.com/list/uuid".
Here's contorller code:
class Listby extends CI_Controller
{
public function index()
{
echo "index";
}
public function userid()
{
echo "userid";
}
public function uuid()
{
echo "uuid";
}
}
Side note: with url like "http://myserver.com/listby/uuid" works fine.
Any clues where is the problem?
try:
$route['list/(:any)'] = "listby/$1";

codeigniter check for user session in every controller

I have this private session in one of my controllers that checks if a user is logged in:
function _is_logged_in() {
$user = $this->session->userdata('user_data');
if (!isset($user)) {
return false;
}
else {
return true;
}
}
Problem is that I have more than one Controller. How can I use this function in those other controllers? Redefining the function in every Controller isn't very 'DRY'.
Any ideas?
Another option is to create a base controller. Place the function in the base controller and then inherit from this.
To achieve this in CodeIgniter, create a file called MY_Controller.php in the libraries folder of your application.
class MY_Controller extends Controller
{
public function __construct()
{
parent::__construct();
}
public function is_logged_in()
{
$user = $this->session->userdata('user_data');
return isset($user);
}
}
Then make your controller inherit from this base controller.
class X extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function do_something()
{
if ($this->is_logged_in())
{
// User is logged in. Do something.
}
}
}
Put it in a helper and autoload it.
helpers/login_helper.php:
function is_logged_in() {
// Get current CodeIgniter instance
$CI =& get_instance();
// We need to use $CI->session instead of $this->session
$user = $CI->session->userdata('user_data');
if (!isset($user)) { return false; } else { return true; }
}
config/autoload.php:
$autoload['helper'] = array('login');
Then in your controller you can call:
is_logged_in();
You can achieve this using helper and CodeIgniter constructor.
You can create custom helper my_helper.php in that write your function
function is_logged_in() {
$user = $this->session->userdata('user_data');
if (!isset($user)) {
return false;
}
else {
return true;
}
}
In controller if its login.php
class Login extends CI_Controller {
public function __construct()
{
parent::__construct();
if(!is_logged_in()) // if you add in constructor no need write each function in above controller.
{
//redirect you login view
}
}
I think using hooks is pretty easy. Just create a hook to check $this->session->user. It will be called in every request.
Get all user's data from session.
In the Controller,
$userData = $this->session->all_userdata();
In the View,
print_r($userData);
I coded like this according to above answers.. And this is running for me
Create file my_helper.php
<?php
function _is_logged_in() {
if(isset($_SESSION['username'])){
return true;
} else {
return false;
}
}
?>
Edit in autoload.php file
$autoload['helper'] = array('my');
In your Controller file
class Welcome extends CI_Controller {
public function __construct(){
parent::__construct();
if(!_is_logged_in())
{
redirect("Login");
}
}
}
Just add this on your folder core file ci_controller at function __construct() to check all controller ():
function __construct()
{
parent::__construct();
if(! $user = $this->session->userdata('user_data');)
{
return false;
}
}

Categories