Search code examples
codeignitercodeigniter-4

Codeigniter 4 routes not working as expected, disorganizes views


I am new to CI4 and i've been battling this for weeks now. I mapped my routes, It is working fine without placeholders, but once i add placeholder, it disorganizes my view.

I have tried several changes to my knowledge but no good result

Routes.php

$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Login');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
$routes->setAutoRoute(true);
$myroutes = [];
$myroutes['login'] = 'Login::index'; //working
$myroutes['logout'] = 'Login::logout'; //working
$myroutes['home'] = 'Home::dashboard'; //working

//User
$myroutes['add_user'] = 'User::create'; //working
$myroutes['view_users'] = 'User::user_list'; //working

//Client
$myroutes['addClient'] = 'Client::create'; //working
$myroutes['clients'] = 'Client::view'; //working
$myroutes['clientProfile/(:num)'] = 'Client::profile/$1'; //not working

$myroutes['rooms'] = 'Home::rooms'; //working
$myroutes['privileges/(:num)'] = 'Home::permissions/$1'; //not working

$routes->map($myroutes);

And here is my controller script. All other methods are working fine except for the profile method Controller (Client.php)

<?php

namespace App\Controllers;
use CodeIgniter\Controller;
use App\Models\ClientModel;

class Client extends BaseController
{
    public $session;
    public $clientModel;
    public function __construct() 
    {
        helper('form');
        $this->clientModel = new ClientModel();
        $this->session = \Config\Services::session();
    }

    public function create()
    {
        if (!session()->has('logged_user')) {
            return redirect()->to('/login');
        }

        $data = [
            'page_name' => 'Add Client',
            'page_title' => 'Add Client | The Ashokas',
        ];
        $data['validation'] = null;

        $rules = [
            'client_title' => 'required|min_length[2]|max_length[5]',
            'first_name' => 'required|min_length[3]|max_length[16]',
            'middle_name' => 'required|min_length[3]|max_length[16]',
            'last_name' => 'required|min_length[3]|max_length[16]',
            'relationship_status' => 'required',
            'client_email' => 'required|valid_email|is_unique[clients.client_email]',
            'mobile' => 'required|numeric|exact_length[11]',
            'gender' => 'required',
            'occupation' => 'required',
            'workplace' => 'required',
            'address' => 'required',
            'kin_name' => 'required|min_length[3]|max_length[50]',
            'kin_relationship' => 'required',
            'kin_mobile' => 'required|numeric|exact_length[11]',
            'kin_address' => 'required'
        ];
    

        if ($this->request->getMethod() == 'post') {
            if ($this->validate($rules)) {
                $clientdata = [
                    'client_title' => $this->request->getVar('client_title'),
                    'first_name' => $this->request->getVar('first_name'),
                    'middle_name' => $this->request->getVar('middle_name'),
                    'last_name' => $this->request->getVar('last_name'),
                    'relationship_status' => $this->request->getVar('relationship_status'),
                    'client_email' => $this->request->getVar('client_email'),
                    'mobile' => $this->request->getVar('mobile'),
                    'gender' => $this->request->getVar('gender'),
                    'occupation' => $this->request->getVar('occupation'),
                    'workplace' => $this->request->getVar('workplace'),
                    'address' => $this->request->getVar('address'),
                    'kin_name' => $this->request->getVar('kin_name'),
                    'kin_relationship' => $this->request->getVar('kin_relationship'),
                    'kin_mobile' => $this->request->getVar('kin_mobile'),
                    'kin_address' => $this->request->getVar('kin_address'),
                    'join_date' => date('d-m-Y')
                ];
                if ($this->clientModel->create($clientdata)) {
                    $this->session->setTempdata('success', 'Client added successfully',3);
                    return redirect()->to('/clients');
                }else{
                    $this->session->setTempdata('error', 'Sorry! Unable to add client',3);
                    return redirect()->to(current_url());
                }
            }else{
                $data['validation'] = $this->validator;
            }
        }
        return view('client/add', $data);
    }

    public function view() {
        if (!session()->has('logged_user')) {
            return redirect()->to('/login');
        }
    
        $data = [
            'page_name' => 'Clients',
            'page_title' => 'All Clients | The Ashokas',
            'clientele' => $this->clientModel->getAllClients()
        ];
        return view('client/view', $data);
    }

    public function profile($client_id = null) {
        if (!session()->has('logged_user')) {
            return redirect()->to('/login');
        }
    
        $data = [
            'page_name' => 'Client Profile',
            'page_title' => 'Client Profile | The Ashokas',
        ];
        return view('client/profile', $data);
    }
}

Expected View expected_view

The view i got the_view_i_got

Genius mind should please assist. Thanks in anticipation


Solution

  • This looks like your javascript and css paths are relative to the current folder in your html header. while you are in /path1/path2 this two level structure works fine but when you have /path1/path2/1 this is three level structure and it fails with relative js and css files.

    Change them to absolute paths.

    You probably have something like this

    <link rel="stylesheet" type="text/css" href="../../somefolder/mycss.css">
    

    Change it to this

    <link rel="stylesheet" type="text/css" href="/appfolder/somefolder/mycss.css">
    

    Do same thing for javascript files and images too.