ajeet singh
ajeet singh

Reputation: 47

how to redirect when a function variable missing

  public function index($id)
   {

    if($id == NULL){  // when  $id value missing then redirect

         return redirect('service');

    }else{
      // some operation perform  

  }

I want to perform some action on id value when id value missing user redirect to service page,value coming from get method here please help me ,this is not working

enter image description here

Upvotes: 2

Views: 81

Answers (4)

Pupil
Pupil

Reputation: 23978

Add a default value NULL for $id in function definition itself.

If the function does not get any value, it will NULL

Check if the function is getting blank $id by using empty() function.

This way, your functionality of redirection will work properly.

Also, your warning will get removed.

public function index($id = NULL) {
 if (empty($id)) {  // when  $id value missing then redirect
  return redirect('service');
 }
 else {
  // some operation perform
 }
}

Upvotes: 2

Ali
Ali

Reputation: 1438

Change your function as below:

public function index($id = '') {
    if(empty($id)){
        return redirect('service');
    }else{
      // perform operation   
    }
}

Upvotes: 1

Sankar V
Sankar V

Reputation: 4128

Change your function as below:

public function index($id = '') {
    if(!trim($id)){  // when  $id value missing then redirect
        return redirect('service');
    }else{
    // some operation perform  
    }
}

Upvotes: 1

6be709c0
6be709c0

Reputation: 8441

You have to tell in your arguments the null value

public function index($id = null)

If no value, $id = null. So you can redirect easily.

Upvotes: 4

Related Questions