• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Redirect vs load view

#1
Question 
When should I use redirect and when
Code:
$this->load->view('projects/page');
?
For example, in the following code snippet what will be more preferable to use?


PHP Code:
class Projects extends CI_Controller
{
   public function __construct(Type $foo null)
   {
       parent::__construct();
       if (!$this->session->userdata('logged_in')) {
           $this->session->set_flashdata('flash_danger''Please login to view this page');
           redirect('home'); // maybe to use `$this->load->view` here?
       }
   }

   public function index()
   {
      ... 


And another question  - am I uderstand it right, that controller object (the object of class  Project) will being created every time I request to this controller?
So will the function  `__construct` be process every time before controller logic will be processed?
Reply

#2
All Classes the __construct() is called first.

Load view loads a view to display the visual stuff.

Redirect directs to another controller / method or another web url.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
(09-17-2018, 04:45 AM)InsiteFX Wrote: All Classes the __construct() is called first.

Do you mean `FOR all classes`?

But how many times that controller creates? 
Every time I request it or it creates only once and then CI reuses it?
Reply

#4
(09-17-2018, 07:59 AM)Serializable Wrote: Do you mean `FOR all classes`?

But how many times that controller creates? 
Every time I request it or it creates only once and then CI reuses it?

When it comes to controllers - 'create' might be confusing term to use.

Controllers are built with PHP classes, but they are only 'loaded' once per page request. If you have multiple methods in one controller (all the different functions), sometimes you might want to apply some checks for every method in this controller - by putting the check code in __construct, you can cut down on amount of work you have to do. In your example it checks if user is logged in, so __construct and redirect are probably most common ways to stop not-logged-in users seeing things they shouldn't.

For redirect v view, this also helps you better structure your code.

Lets say there's a specific list, lets say latest news, you need to load on your home page. Instead of saying, if user has not logged in, I now have to remember to load in this list, you can just send users browser to home page controller via redirect, it enforces users browser to load a specific URL. It creates another request, but also you only have one place that you'd have to handle that specific list of items.
Reply

#5
A good way to demonstrate the difference between load->view and redirect is when you handle a form and it's input.
A very basic example:
PHP Code:
public function edit_user($id)
{
 
   if (! $this->input->post('ok_button')) {
 
      $data $this->user_model->get_record_of_user($id);  //get data from a model
 
      $this->load->view('frm_edit_user',$data);    //load the form
 
   }
 
   else {
 
        $data = array(
 
          'name' => $this->input->post('name'),
 
          'city' => $this->input->post('city')
 
       );
 
       $this->user_model->update($data,$id);   //update the record via a model
 
       redirect('users/users_list');   //open the url that displays the users list
 
   }



So, the load->view is used to show the view (in this case a form) that belongs to the edit_user method.
The redirect is used to open another url after some action has been completed (in this case saving the post data from the form).
Reply

#6
(09-16-2018, 11:22 AM)Serializable Wrote: And another question  - am I uderstand it right, that controller object (the object of class  Project) will being created every time I request to this controller?
So will the function  `__construct` be process every time before controller logic will be processed?

Yes, to both your questions.

It might be useful to recall how webservers work. Every time it receives a URL, whether it is from a user typing it or from a redirect call, the server starts a new process from scratch. The "process" has no knowledge of any other requests the server is handling or has handled.

The server uses the URL to determine which script files to load, allocates memory for them, runs them and then sends the output of the script to the browser. The "output" might be a request to go to some other URL (a redirect) or some HTML to display, or some AJAX data for the browser to consume. When the server runs out of code to execute (or is told to "exit" the script) the server forgets all about the request. At that point any memory allocated is released and the webserver "process" that handled the request ends. If you have ever read about the web being "stateless" the above is why - webservers to not remember the "state" of any given request.

A "redirect()" call is using code to accomplish the same thing typing a URL in a browser does. The script that calls redirect() ends (redirect calls exit) and the server cleans up that process.

Yes, the same code gets called again and again and again. But that's one of the main things computers are good at.
Reply

#7
ok, why does the autor pass a parameter in the constructor and assing null to it? what he's trying to achive?

and why does the parameter prefaced by `Type`?
is `Type` a so called generic (in Java terms)?
Reply

#8
(09-17-2018, 01:25 PM)Serializable Wrote: ok, why does the autor pass a parameter in the constructor and assing null to it? what he's trying to achive?

and why does the parameter prefaced by `Type`?
is `Type` a so called generic (in Java terms)?

I'm not positive about the 'Type', but guess it is there as a placeholder for a Type Declaration - a new PHP feature in 7.0.

The ($foo = null) is an example of a default argument - a very common feature in many programming languages where, if no argument value is passed to the call the default will be assigned. In this specific case, if no argument is passed to the constructor $foo will be given a value of null.
Reply

#9
I think from a programmer's POV a redirect to a controller is easier to read and maintain as a controller can execute a bunch of things in one go.

Now the bad things is you want to limit http requests.

I think best practice is an example with form validation.

If form validate fails, load a view file (this is necessary if you are using flash data)
If it is successful do a redirect to a controller.

^I mean this is what I do. I'd be interested to know if this is best practice though. It just seemed logical to me.
Practical guide to IgnitedCMS - Book coming soon
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2018 MyBB Group.