CodeIgniter Forums

Full Version: Remove "Welcome View" (CI 4)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey,

I just started using CodeIgniter and my initial goal is to remove the default "index" (welcome_message)

Firstly, I deleted the Controller "Home.php" and created a "Pages.php" Controller with the following content:

PHP Code:
<?php

namespace App\Controllers;

use 
CodeIgniter\Controller;


class 
Pages extends Controller{

    public function index()
    {


    }


    public function renderPage($page "home")
    {
        #Render index Page
        if($page == ""){

            return redirect()->to(base_url().'public/home');

        }else{


            # Render everything else
        if(!is_file(APPPATH."/Views/pages/".$page.".php")){
            throw new \CodeIgniter\Exceptions\PageNotFoundException($page);
        }


        $data['title'] = ucfirst($page); // Capitalize the first letter
        $data["theme"] = "sms_default/bootstrap.css";

        echo view('templates/header'$data);
        echo view('pages/'.$page$data);
        echo view('templates/footer'$data);

    }
    }





Then I removed the default Route and replaced it with my own:

PHP Code:
/**
 * --------------------------------------------------------------------
 * Route Definitions
 * --------------------------------------------------------------------
 */

// We get a performance increase by specifying the default
// route since we don't have to scan directories.

$routes->get('(:any)''Pages::renderPage/$1');

/**
 * --------------------------------------------------------------------
 * Additional Routing
 * --------------------------------------------------------------------
 *
 * There will often be times that you need additional routing and you
 * need to it be able to override any defaults in this file. Environment
 * based routes is one such time. require() additional route files here
 * to make that happen.
 *
 * You will have access to the $routes object within that file without
 * needing to reload it.
 */
if (file_exists(APPPATH 'Config/' ENVIRONMENT '/Routes.php'))
{
    require 
APPPATH 'Config/' ENVIRONMENT '/Routes.php';



As you see I'm using a quite "dirty" way to check if the user is on the index site, 
I cant use the "index()" function, because it always returns a 404.

What am I doing wrong?
You still need to have a Default Controller. This will also change the default index() into renderPage(). So that you don't need multiple functions.

Your code makes you vulnerable to directory traversal attack. You should read up on that!

PHP Code:
$routes->setDefaultController('Pages');
$routes->setDefaultMethod('renderPage'); 
(02-01-2020, 07:27 AM)jreklund Wrote: [ -> ]You still need to have a Default Controller. This will also change the default index() into renderPage(). So that you don't need multiple functions.

Your code makes you vulnerable to directory traversal attack. You should read up on that!

PHP Code:
$routes->setDefaultController('Pages');
$routes->setDefaultMethod('renderPage'); 

Thank you, that did the trick! Smile