Welcome Guest, Not a member yet? Register   Sign In
Issue with the controller arguement
#1
Exclamation 
(This post was last modified: 07-10-2018, 06:45 PM by scalla.)

good day @kilishan
based on the refactoring of the controller class about 21 hours ago, removing the use of constructor from it, how is the (request, response) object going to be used in other controllers?? prior to the refactoring, it was straight forward.
Reply
#2

It's still pretty straight forward. Instead of being passed in through the constructor it's passed in through the initController() method, which is identical to what the constructor used to be. So, $this->response still works, unless you're in the constructor.

Besides that, the response and request objects are shared instances through the Services class so the current one can always be retrieved with $request = Config\Services::request();.
Reply
#3

oh i get, but why the revert from it previous way?? is it due to the redirect issue!!
Reply
#4

Honestly, not sure it changes the redirect issue. Wasn't trying to fix that since Filters does most of what people are trying to redirect in constructors for...

Mainly it was to clean things up a bit, and make it a little simpler for developers. It sounds like it's causing you pain, though. What are you running into?
Reply
#5

(This post was last modified: 07-11-2018, 05:54 AM by scalla.)

the redirect does not work for the filter.
below is my sample code for staff authentication,if i use raw head location which i commented below, it works well.
in my observation, seems there is an issue with the sent out header.
PHP Code:
<?php namespace App\Filters;

use 
Config\Services;
use 
CodeIgniter\Events\Events;
use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
CodeIgniter\Filters\FilterInterface;
class 
StaffAuth implements FilterInterface
{
    public function 
before(RequestInterface $request)
    {
        
$auth Services::Auth('Staff');

        if (
$request->uri->getSegment(3) != 'login')
        {
            if (!
$auth->isLoggedIn())
            {
         
     if ($request->isAJAX())
         
     {
         
         $this->jsonresponse = [
                     
  'status'      => 'error',
                     
  'redirectUrl' => route_to('StaffLogin'),
                    ];
                    return 
Services::response()->setJson($this->jsonresponse);
         
     }
         
     //return redirect(route_to('StaffLogin').'?redirect_to='.urlencode($request->detectPath('REQUEST_URI')));
         
     //header('Location: ' . site_url(route_to('StaffLogin')), TRUE, 302);
                //exit;
         
     return redirect()->route('StaffLogin');
         
  }
         
  Events::trigger('Auth.loggedin', []);
        }
    }

 
 //--------------------------------------------------------------------

    
public function after(RequestInterface $requestResponseInterface $response)
    {
        
// Do something here
    
}

Reply
#6

Hm. Used to work, but it's been a while since I've been in that code. Please file an issue over at Github or else this will get lost in the shuffle.
Reply
#7

just done that
Reply
#8

(This post was last modified: 07-11-2018, 08:43 AM by enlivenapp.)

Sorry for the probably dumb question... between this and the other thread I'm still somewhat confused.

So do we use `intController()` now instead of `__contsruct()` when we need to automatically set things up controller-wide? (like setting class members, etc?)

Additionally, is this change only in controllers, or will it move out to models, libraries, etc?
Reply
#9

(07-11-2018, 08:42 AM)enlivenapp Wrote: Sorry for the probably dumb question... between this and the other thread I'm still somewhat confused.

So do we use `intController()` now instead of `__contsruct()` when we need to automatically set things up controller-wide? (like setting class members, etc?)

lol, not a dumb one, if i get @kilishan properly, you do not have to use the. intController(), just instantiate the needed object in your site wide controller. though i feel its better with the constructor than the new buddy.
Reply
#10

Correct - you do not need to even think about the initController method. That's called by CodeIgniter when the Controller is instantiated, and just frees you from having to worry about constructor arguments whenever you need to do something in your controller's constructor.

And the "redirect issue" in the controller constructor isn't able to be fixed as you cannot return another object from a class constructor. You can always use filters (once investigated and fixed) or the _remap method in the controller.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB