Welcome Guest, Not a member yet? Register   Sign In
how to use controllers filters on CI4
#3

(This post was last modified: 01-12-2017, 09:44 AM by casa.)

Hye
Thanks you for your explaination, but i have always the same problem.
I don't understand Sad

# _________________
# I created this file on :

// application/Filters/AuthFilter.php
PHP Code:
namespace App\Filters;
use 
CodeIgniter\Config\Services;
use 
CodeIgniter\Filters\FilterInterface;
use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;

class 
AuthFilter implements FilterInterface {

 
   public function before(RequestInterface $request) {
 
       // appel du service 'auth'
 
       $auth = \Config\Services::authentif();   //service('authentif') doesn't work in my code :(... i don't know why :(
 
       if ($auth->isLoggedIn() === true) {
 
          echo 'hello';
 
       } else {
 
           echo 'nothing';
 
       }
 
   }

 
   public function after(RequestInterface $requestResponseInterface $response) {
 
   }


#____________________
# Then, i created the file on application/Libraries/AuthLibrary.php
PHP Code:
namespace App\Libraries;
class 
AuthLibrary
{
 
   private $session;
 
   public function __construct(){
 
      $this->session = \Config\Services::session(); 
// for information, i config App.php for session storage in files (WRITEPATH.'sessions_cache', with directory named "sessions_cache" and no problem at this level
 
   }

 
   public function isLoggedIn(){
 
       return ($this->session->get('key')!== null)?TRUE:FALSE;
 
   }


#_____________________________
# Then, on application/Config/Services.php, i created this function :
PHP Code:
public static function authentif($getShared=false){
 
       if($getShared){
 
           return self::getSharedInstance('authlibrary');
 
       }
 
       return new \App\Libraries\AuthLibrary();
 
   

#_______________
# i created controller on application/Controllers/Test/Essai.php who extends Controller and i add directory Test as you can see. The code of Essai.php is :
PHP Code:
use CodeIgniter\Controller;

class 
Essai extends Controller
{
 
   private $session;

 
   public function __construct(...$params)
 
   {
 
       parent::__construct(...$params);
 
       session()->start();
 
   }
 
   
    public 
function index(){
 
       $this->session = \Config\Services::session();
 
       $this->session->set('key','12345');
 
       return view('essai_view');
 
   }


#______________________
# my essai_view.php file
PHP Code:
$session = \Config\Services::session();
echo 
'<br/><br/>var de session = '.($session->get('key')!==null?$session->get('key'):'aucune valeur').'<br/>'

#__________________________________
# Then on application/Config/Filters.php, i have many problems. I put !!! before my questions below.
PHP Code:
namespace Config;

use 
CodeIgniter\Config\BaseConfig;

class 
Filters extends BaseConfig
{
 
// Makes reading things below nicer,
 // and simpler to change out script that's used.
 
public $aliases = [
 
'csrf'  => \App\Filters\CSRF::class,
 
'toolbar' => \App\Filters\DebugToolbar::class,

 
// config of my filter
 
'authentification' => \App\Filters\AuthFilter::class,
 
 ];

 
// Always applied before every request
 
public $globals = [
 
'before' => [
 
// 'csrf'
     
'authentification'                  // if i understand before every request "authentification" is called.
  
],
 
'after'  => [
 
'toolbar'
 
]
 ];

 
// Works on all of a particular HTTP method
 // (GET, POST, etc) as BEFORE filters only
 //     like: 'post' => ['CSRF', 'throttle'],
 
public $methods = [];   // here for only method http                      

 // List filter aliases and any before/after uri patterns
 // that they should run on, like:
 //    'isLoggedIn' => ['before' => ['account/*', 'profiles/*']],
 
public $filters = [
 
    /* to make simple :
         'nameOfAliasFilter' => ['before' => [list of controllers or directory controller (ex: admin/* for all controllers in application/Controllers/Admin/) ]]  
     */

 
];


#_____________________________________________________
# With application/Config/Filters.php like above, i have this result when i call my controller "essai" :
PHP Code:
nothing

var de session 12345

// i thought that i should have "hello" and after "var de session = 12345". 
#_____
Thanks of your help. Sorry for my english, i am french.
Reply


Messages In This Thread
how to use controllers filters on CI4 - by casa - 01-10-2017, 02:46 PM
RE: how to use controllers filters on CI4 - by casa - 01-11-2017, 02:15 PM



Theme © iAndrew 2016 - Forum software by © MyBB