Welcome Guest, Not a member yet? Register   Sign In
Filter Group Codeigniter 4
#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


Messages In This Thread
RE: Filter Group Codeigniter 4 - by InsiteFX - 03-20-2021, 03:54 AM
RE: Filter Group Codeigniter 4 - by Rivaldy Saputra Agus - 03-20-2021, 05:12 PM



Theme © iAndrew 2016 - Forum software by © MyBB