Php codeigniter:Passing empty arguments through redirect - php

Here is my controller function
Class Customer Extends CI_controller{
public function view($page='customer-new-bill',$param=null,$type='normal')
{
}
}
Sometimes i don't want have a value for $param argument so i just empty the second parameter using this way in redirect
redirect('customer/view/customer-view-invoice//invoice');
But it didn't worked.
I want to pass empty parameters through redirect is it possible?
Note:
I am used this method $this->view('','','invoice') its worked but i need a redirect

if you want to send the null parameters then send at the end
Class Customer Extends CI_controller{
public function view($page='customer-new-bill',$type='normal', $param=null)
{
}
}
and use this route
$route['customer/view/(.*)'] = "customer/view/$1/$2";
The route (.*) work for unlimited perimeters.

It is good to have the default and null parameters at the end
Class Customer Extends CI_controller{
public function view($page='customer-new-bill',$type='normal', $param=null)
{
}
}
so now redirect
redirect('customer/view/customer-view-invoice/invoice/');
you can create routes too if you want
$route['customer/view/(:any)/(:any)'] = "customer/view/$1/$2";
$route['customer/view/(:any)/(:any)/(:any)'] = "customer/view/$1/$2/$3";

Related

How to pass parameter after baseurl in codeigniter?

Base URL: http://localhost/f1/
and I am passing parameter in URL like:
http://localhost/f1/user1
and I am trying to print
function index(){
echo $this->uri->segment(2);
}
I want to print User1 in the controller. How to achieve this?
Config your rout and add this:
$route['Controller_Name/(:any)'] = 'Controller_Name/index/$1';
Here you go with your controller
class Controller_Name extends CI_Controller {
function index($prm){
echo $prm;
}
}
Have fun....
you can have how many uri you want....
Read here https://www.codeigniter.com/userguide3/general/controllers.html -> Passing URI Segments to your methods
/products/shoes/sandals/123
<?php
class Products extends CI_Controller {
public function shoes($sandals, $id)
{
echo $sandals;
echo $id;
}
}
After base_url segemnts/parameter can get like this
Make sure base_url and URL helper is set in the application/config/config.php and application/config/autoload.php file respectively
Suppose, calling a function with a anchor tag
Index
In the above line, HomeController is controller name, index in the function name and $url1 is the parameter
class HomeController extends CI_Controller {
public function index($url1){
echo $url1;
}
}
Also, $this->uri->segment() can be used.
What i understood from your question is that you want a remap without using your method and pass the parameter directly after your controller?
You can user _remap and check if it matched a method process it normally or pass it to your default method directly and now you won't need to use index in your url as you intended.
public function _remap($method)
{
if ($method === 'some_method_in_your_controller')
{
$this->$method();
}
else
{
$this->index($method);
}
}
Now lets say your url is like this http://localhost/controller/parameter then if this parameter matches a method it will call that method if not it will pass it as a parameter to you index.
define your controller in config/routes.php.it will call your index function by default.
$route['default_controller'] = 'controllername';
check whether you loaded the url helper. Only then it will work as expected. Either auto-load it or call it in the controller.

How to pass a variable from one function to another function in same controller in codeigniter

I have tried to pass a variable form a function to another function in same controller in codeigniter. I have used following code
redirect('fornt/fullcommunity',$project_encode_id);
here fornt is the name of the controller,
full community is the function inside the controller
$project_encode_id is the variable to pass
but it is not working
In your case I would replace the redirect sentence with the following one:
$this->load->view(‘funLoadView’,$requiredVarForView);
Where the first parameter is the function in your controller, and the second one the data (possible array) that you need to include.
Both functions are on the same controller class so you don’t need to include any reference to the controller class name
firsst you need to add your controller code.
Then, if bouth methods are in the same controller you only need to do this
class Your_controller extends CI_Controller{
public function method1(){
$variable_form = $this->input->post('form_variable', TRUE);
//calling second method
$response = $this->_method2($variable_form);
}
private function _method2 ( $str ){
return 'do somthing -> ' . $str;
}
}

Codeigniter: Using url segments in a query

I just started working with CodeIgniter and I am having some trouble with the segment-based urls. I understand how to call them doing $variable = $this->uri->segment(2); but whenever I go to the url, I am getting a 404. Is there something I need to do for URI routing?
For example, I am trying to go to localhost/ci/index.php/games/1000 (where 1000 would be a game ID), but I am getting a 404. localhost/ci/index.php/games/ works fine.
In order for that to work you would need to have a controller called games.php with this content
class Games extends CI_Controller
{
public function index($id)
{
echo $id;
}
}
Unless you do something like this
class Games extends CI_Controller
{
public function index()
{
echo 'this is index';
}
public function game($id)
{
echo $id;
}
}
and add this to your routes.php
$route['game/(:any)'] = "games/game/$1";
By default the 2nd segment of the URI is a method (function) within the controller which CI automatically calls.
So in your case you are actually attempting to call a function named 1000() within the games controller, which doesn't exist and therefore results in a 404.
Instead what I think you want to do is call the index() function, and pass the variable 1000 to it.
So if you were to go to localhost/ci/index.php/games/index/1000 you shouldn't get a 404 anymore, however your URI segment will now be wrong to get the variable 1000.
Here is a working example of the controller with the corrected URI segment:
class Games extends CI_Controller
{
// good habit to call __construct in order to load
// any models, libraries, or helpers used throughout this controller
public function __construct()
{
parent::__construct();
}
// default controller
public function index()
{
// this should display 1000
echo $this->uri->segment(3);
}
}

Same data variable passing to view

In my project, I have one search section with 3 select box. I passed the value to it using
$data['restaurant_all']=$this->restaurant_model->get_all('','','','','','yes')->result();
$data['state_all']=$this->state_model->get_all();
$data['menu_all']=$this->menu_model->get_all('all','','','','','','yes')->result();
$data['restaurant']=$this->input->post('restaurant');
$data['state']=$this->input->post('area');
$data['food_type']=$this->input->post('menu');
I need this statement in all my pages. In there any way to accomplish this without writing these statements in all the pages
a. extend the default controller by creating a file MY_Contoller.php at a suitable location.
b. create a custom class that will extend the default controller.
c. add a protected or public variable $data to custom class.
e. do something with data using __construct()
d. make every controller extend the custom controller.
e. you can access this variable like any other class variable.
example code:
MY_Controller.php
class APP extends CI_controller {
protected $data;
function __construct() {
parent::__construct();
$this->_init();
}
function _init() {
$this->data['state'] = $this->input->post('area');
}
}
normal controllers:
class Welcome extends APP {
function __construct() {
parent::__construct();
}
function view() {
/* pass this data value like normal data param */
$this->load->view('some_view', $this->data);
}
}
hope it helps.
Use constants, in /config/constants.php

How to pass parameters in index function in codeigniter

here's an example of url:
http://www.example.com/search/search-keyword
I am trying to make this work, I removed the index.php using .htaccess, search is the controller and I want to pass a parameter in the index method.
this is currently what it looks like:
class Search extends CI_Controller{
function index($param){
echo $param;
}
}
any suggestions? I can't seem to make it work
You need the index segment http://www.example.com/search/index/search-keyword.
Or you need to use a route $route['search/(:any)'] = 'search/index/$1';
Or you can look at remap
Remember not to trust user input, especially when you are throwing it into your url. The latest version of CI supports $_GET variables now, so you may want to look into using that or flashdata. A searh term as simple as O' Brien will give you a fatal error ("The URI you submitted has disallowed characters.").
class Search extends CI_Controller{
function _remap($param) {
$this->index($param);
}
function index($param){
echo $param;
}
}
You should then be able to access that as: /search/123123 :and the page would echo out "123123" or whatever you put in place of that.
function _remap($parameter){
$this->_index($parameter);
}
Give it a try and tell us how it went.
Most of these solutions will only work if you only have a single function called index() in your controller. If you have this in your routes:
$route['search/(:any)'] = 'search/index/$1';
With the above in your routes, what will happen is that your search function will work but then if you add any other functions to that same controller they'll all be redirected to /search/index/$1.
The only solution I can think of that allows you to use the URL you want while still being able to add any other functions to your search controller is to add a sort of messy conditional to your routes file. Here's what it will look like and what it does:
$request = $_SERVER['REQUEST_URI']; // Only add this for readability
if(!strpos($request, 'search/another_function') || !strpos($request, 'search/more_functions')) {
$route['search/(:any)'] = 'search/index/$1';
}
What this is doing is basically saying "if the requested URL doesn't contain the name of any of my search controller functions then it must be meant for the index so we'll activate the route rule for index".
This will allow you to take requests for search/any_other_functions_you_have without issue and only activate the rule for hiding the index function when a request URI doesn't match any of them.
One side effect of this is that you'll never get a 404. For example, if someone enters a URL like "yourdomain.com/search/something" and they expect it to show a non-search result page they won't get a 404 alerting them to the fact that there is no page like that and instead the app will assume what they typed is a search term. However, it sounds like this isn't much of an issue for you and I don't see it being such a terrible thing for one controller to be unabe to return 404s.
You need to understand the way code igniter urls work, its basically like this:
http://www.mysite.com/{controller}/{function}
So what your url is actually looking a function called "keyword" in your search controller.
You have multiple options. The easiest will be to simply change the url to:
http://www.mysite.com/search/result/keyword
Then this should work perfectly:
class Search extends CI_Controller{
function result($param){
echo $param;
}
}
If you really want to use the url as you had it, you can use the same snippet of code as above but also open up "application/config/routes.php" and add this to the bottom.
$route['search/(:any)'] = "search/result";
Alternatively if you want to continue using the index function you can change it to this
$route['search/(:any)'] = "search/index";
And change your class to something like this:
class Search extends CI_Controller{
function index($param=FALSE){
if($param == FALSE) {
//Show search box
} else {
//Show search results
}
}
}
Don't forget to update your answer if you figure it out yourself or accept somebodies answer if it answers it :)
I've just started CI and here was my solution and implemented it on my website. So far it works for me and my search queries have been indexed by google as links
Class Search extends CI_Controller{
function index(){
$search_item = $this->input->post('search_box'); // this is from the input field
redirect(base_url()."search/q/".url_title($search_item));
}
// i've redirected it to the "search" controller then :
function q($search_item = null){// process search
// load the view here with the data
}
}
This is my solution:
From CI userGuide
public function _remap($method)
{
if ($method == 'some_method')
{
$this->$method();
}
else
{
$this->default_method();
}
}
So I've created my controller in this way:
class Shortener extends CI_Controller {
function shortener() {
parent::__construct();
}
function index($getVar) {
echo "Get var: " . $getVar;
}
function config() {
echo "another function";
}
function _remap($method) {
if($method == "config") {
$this->$method();
}
else {
$this->index($method);
}
}
}
Hope this can be helpful to someone...
BUG: if you call /shortener _remap function pass "index" to index() function...it can be solved adding an if condition or something else
You should use the CI URI Class. http://codeigniter.com/user_guide/libraries/uri.html
class Search extends CI_Controller{
function index($param){
//echo the search-keyword
echo $this->uri->segment(2);
}
}
This easiest way is just to use uri segments:
class Search extends CI_Controller{
function index(){
$param=$this->uri->segment(2);
echo $param;
}
}
Or like Madmartigan said, use routes, which is probably the better way of doing this.
I had to also check if some variable was passed or not - so here is the solution that I took.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Story extends CI_Controller{
function story() {
parent::__construct();
}
function _remap($parameter){
$this->_index($parameter);
}
public function _index($story_id) {
if($story_id == 'index'){
//No variable passed
redirect('auth', 'refresh');
}else{
$data['title'] = "Story";
$this->load->view('story', $data);
}
}
}
Hope this helps someone!
Thank you. This code works for me. If parameter is a number
Or you need to use a route $route['search/(:any)'] = 'search/index/$1';
However, If the parameter is a string (http://[::1]/note/getnote/index/fg => http://[::1]/note/getnote/fg). I use _remap() like this code.
<?php
class Getnote extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('image_lib');
}
public function index(){
}
public function _remap($keyname)
{
$this->load->model('Laydb');
$data = $this->Laydb->getNote($keyname);
$proListArray['patitle'] = "Edit notepad";
$this->load->view('top', $proListArray);
$this->load->view('editblog', $data);
$this->load->view('bottom');
}
}
class Search extends CI_Controller{
function index($param){
echo $param;
}
}
In routes.php
$routes['search/(:any)'] = 'search/index/$1';
It's all
I finally found a workaround since I cant simply put post variables into the URL.
What I did was create another function, then redirect it to that function.
class Search extends CI_Controller{
function index(){
$search_item = $this->input-post('search_item');
redirect("search/q/".url_title($search_item));
}
function q($key){
// process search
}
}

Categories