Welcome Guest, Not a member yet? Register   Sign In
Filter Group Codeigniter 4
#1

(This post was last modified: 03-19-2021, 11:45 PM by Rivaldy Saputra Agus.)

Hi everyone, I am new to codeigniter version 4.1.1, I want to ask about Routes Filter Group. I created 1 filter for authentication for the authority of each user that has been specified. then I have a routes group like this:
Code:
$routes->group('/halaman', function($routes){
$routes->get('/beranda', 'Halaman::beranda');
$routes->get('/profil', 'Halaman::profil');
$routes->get('/galeri', 'Halaman::galeri');
$routes->get('/tentang', 'Halaman::tentang');
$routes->get('/FAQ', 'Halaman::FAQ');
$routes->get('/cari', 'Halaman::cari');
});


Then I filter the routes group like this:
Code:
$routes->group('/halaman', ['filter' => 'cekauth'], function($routes){

    $routes->get('/beranda', 'Halaman::beranda');
    $routes->get('/profil', 'Halaman::profil');
    $routes->get('/galeri', 'Halaman::galeri');
    $routes->get('/tentang', 'Halaman::tentang');
    $routes->get('/FAQ', 'Halaman::FAQ');
    $routes->get('/cari', 'Halaman::cari');

});
However, the filter does not work, I have followed all the rules in creating a Filter

But if I apply it like this:

Code:
$routes->get('/halaman/beranda', 'Halaman::beranda', ['filter' => 'cekauth']);
It functions the way I want it. How to solve this? please help.
These are My Filters :


Code:
private $sesi;

    public function before(RequestInterface $request, $arguments = null)
    {
        $this->sesi = session();
        if(!is_null($this->sesi->get('masuk'))){
            if($this->sesi->get('wewenang') === "administrator"){
                return redirect()->to('/beranda/administrator');
            }elseif($this->sesi->get('wewenang') === "developer"){
                return redirect()->to('/beranda/developer');
            }else{
                if($this->sesi->get('wewenang') === "konsumen"){
                    return redirect()->to('/beranda/konsumen');
                }
            }
        }
    }
Reply
#2

(This post was last modified: 03-20-2021, 01:38 AM by Rivaldy Saputra Agus.)

#SOLVED the solution has to add a route to the index method. so I have to create an index method. previously I didn't make the index method the default method.
Reply
#3

Please show the code here for others that may need it also.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

These are my Routes.

PHP Code:
$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Halaman');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
$routes->setAutoRoute(true);

/*
 * --------------------------------------------------------------------
 * Route Definitions
 * --------------------------------------------------------------------
 */

// We get a performance increase by specifying the default
// route since we don't have to scan directories.
$routes->get('/''Halaman::index');

// Halaman
$routes->group('halaman', ['filter' => 'cekauth'], function($routes){

    
$routes->get('beranda''Halaman::beranda');
    
$routes->get('profil''Halaman::profil');
    
$routes->get('galeri''Halaman::galeri');
    
$routes->get('tentang''Halaman::tentang');
    
$routes->get('FAQ''Halaman::FAQ');
    
$routes->get('cari''Halaman::cari');

});
// Akhir Halaman

// Auth
$routes->post('login''Auth::login');
$routes->get('keluar''Auth::keluar');
// AKhir Auth

// Admin
$routes->get('beranda/administrator''Administrator::beranda', ['filter' => 'auth']);
// Akhir Admin

// Pengguna
$routes->group('pengguna', ['filter' => 'auth'], function($routes){

    
$routes->get('tampil-data-pengguna''Pengguna::tampil');
    
$routes->get('cari-data-pengguna''Pengguna::cari');
    
$routes->get('tambah-data-pengguna''Pengguna::tambah');

});
// Akhir Pengguna

// Developer
$routes->get('beranda/developer''Developer::beranda', ['filter' => 'auth']);
// Akhir Developer

// Developer
$routes->get('beranda/konsumen''Konsumen::beranda', ['filter' => 'auth']);
// Akhir Developer 

Previously I set the Default Method with the name home and not with the name index. Now here's the problem, because my base_url is in .env = http: // localhost: 8080 / when I access the URL with localhost: 8080, codeigniter will look for it with the default index method not with the defaulf method that I set myself. Therefore, I keep creating an index method on my controller, then the index method will return a redirect to the first page I want to appear, this solution is perfect for the route group that I want to filter.

My Controller :
Code:
<?php

namespace App\Controllers;

use App\Controllers\BaseController;
use Config\Services;

class Halaman extends BaseController
{
    public function __construct()
    {
        helper('form');
    }

    public function index()
    {
        return redirect()->to('/halaman/beranda');
    }

    public function beranda()
    {    
        $uri      = ucwords(Services::request()->uri->getSegment(2));
        $title    = $uri." | ".$this->nama_aplikasi;
        $validasi = Services::validation();
        return view('beranda', compact('uri','title','validasi'));
    }

    public function profil()
    {
        $uri      = ucwords(Services::request()->uri->getSegment(2));
        $title    = $uri." | ".$this->nama_aplikasi;
        $validasi = Services::validation();
        return view('profil', compact('uri','title','validasi'));
    }

    public function galeri()
    {
        $uri      = ucwords(Services::request()->uri->getSegment(2));
        $title    = $uri." | ".$this->nama_aplikasi;
        $validasi = Services::validation();
        return view('galeri', compact('uri','title','validasi'));
    }

    public function tentang()
    {
        $uri      = ucwords(Services::request()->uri->getSegment(2));
        $title    = $uri." | ".$this->nama_aplikasi;
        $validasi = Services::validation();
        return view('tentang', compact('uri','title','validasi'));
    }

    public function FAQ()
    {
        $uri      = ucwords(Services::request()->uri->getSegment(2));
        $title    = $uri." | ".$this->nama_aplikasi;
        $validasi = Services::validation();
        return view('FAQ', compact('uri','title','validasi'));
    }

    public function cari()
    {
        $uri       = ucwords(Services::request()->uri->getSegment(2));
        $title     = $uri." | ".$this->nama_aplikasi;
        $data_cari = $this->request->getGet('data');
        $validasi  = Services::validation();
        return view('cari', compact('uri','title','data_cari','validasi'));
    }
}

And My Filter CekAuth
PHP Code:
private $sesi;

    public function 
before(RequestInterface $request$arguments null)
    {
        
$this->sesi session();
        if(
$this->sesi->get('wewenang') === "administrator"){
            return 
redirect()->to('/beranda/administrator');
        }elseif(
$this->sesi->get('wewenang') === "developer"){
            return 
redirect()->to('/beranda/developer');
        }else{
            if(
$this->sesi->get('wewenang') === "konsumen"){
                return 
redirect()->to('/beranda/konsumen');
            }
        }
    } 

Hopefully it can help for those who have problems like me: D
Reply




Theme © iAndrew 2016 - Forum software by © MyBB