• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Setting a homepage

#1
Hi,

I organized my controllers and views into subdirectories like this

frontend
admin

I created a Home.php in controllers/frontend
and a
home.php file in views/frontend

CI is installed in http://localhost/ci

when I access this url I'd like the home view to load, but currently I can only access it via http://localhost:8080/ci/frontend/home

I'm guessing it may have something to do with my .htaccess file which is this:


Code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

What am I doing wrong?

Thanks
Reply

#2
application\routes.php
$route['default_controller'] = 'frontend/home';
Reply

#3
(03-28-2018, 02:09 PM)jreklund Wrote: application\routes.php
$route['default_controller'] = 'frontend/home';

Yeah, I already set this, but it's still not working.
Reply

#4
The default controller cannot be in a sub-directory.

From the CodeIgniter Users Guide.

You can NOT use a directory as a part of this setting!


PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

/**
 * -----------------------------------------------------------------------
 * Editor   : PhpStorm
 * Date     : 1/12/2018
 * Time     : 7:40 AM
 * Authors  : Raymond L King Sr.
 * -----------------------------------------------------------------------
 *
 * Class        MY_Controller
 *
 * @project     ci3admin
 * @author      Raymond L King Sr.
 * @link        http://www.procoversfx.com
 * @copyright   Copyright (c) 2009 - 2018 Custom Software Designers, LLC.
 * @license     http://www.procoversfx.com/license
 * -----------------------------------------------------------------------
 */

/**
 * Class BaseController
 */
class BaseController extends CI_Controller
{
    
/**
     * Class properties - public, private, protected and static.
     * -----------------------------------------------------------------------
     */

    /**
     * Data variable for views.
     *
     * @var  array
     */
    
public $data = [];

    
/**
     * __construct ()
     * -----------------------------------------------------------------------
     *
     * Class    Constructor
     *
     * NOTE: Not needed if not setting values or extending a Class.
     */
    
public function __construct()
    {
        
parent::__construct();

        
log_message('debug'"BaseController Controller Class Initialized");
    }

    
// -----------------------------------------------------------------------

  // End of BaseController Controller Class.

/**
 * Class Backend
 */
class Backend extends BaseController
{
    
/**
     * Class properties - public, private, protected and static.
     * -----------------------------------------------------------------------
     */

    /**
     * __construct ()
     * -----------------------------------------------------------------------
     *
     * Class    Constructor
     *
     * NOTE: Not needed if not setting values or extending a Class.
     */
    
public function __construct()
    {
        
parent::__construct();

 
       log_message('debug'"Backend Controller Class Initialized");
    }

    
// -----------------------------------------------------------------------

  // End of Backend Controller Class.

/**
 * Class Frontend
 */
class Frontend extends BaseController
{
    
/**
     * Class properties - public, private, protected and static.
     * -------------------------------------------------------------------
     */

    /**
     * __construct ()
     * -------------------------------------------------------------------
     *
     * Class    Constructor
     *
     * NOTE: Not needed if not setting values or extending a Class.
     */
    
public function __construct()
    {
        
parent::__construct();

        
log_message('debug'"Frontend Controller Class Initialized");
    }

    
// -------------------------------------------------------------------

  // End of Frontend Controller Class.

// End of MY_Controller Controller Class.

/**
 * -----------------------------------------------------------------------
 * Filename: MY_Controller.php
 * Location: ./application/core/MY_Controller.php
 * -----------------------------------------------------------------------
 */ 

Use the above and then create a Home controller.

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

/**
 * -----------------------------------------------------------------------
 * Editor   : PhpStorm
 * Date     : 1/12/2018
 * Time     : 7:29 AM
 * Authors  : Raymond L King Sr.
 * -----------------------------------------------------------------------
 *
 * Class        Home
 *
 * @project     ci3admin
 * @author      Raymond L King Sr.
 * @link        http://www.procoversfx.com
 * @copyright   Copyright (c) 2009 - 2018 Custom Software Designers, LLC.
 * @license     http://www.procoversfx.com/license
 * -----------------------------------------------------------------------
 */
class Home extends Frontend
{
    
/**
     * Class properties - public, private, protected and static.
     * -----------------------------------------------------------------------
     */

    /**
     * __construct ()
     * -----------------------------------------------------------------------
     *
     * Class    Constructor
     *
     * NOTE: Not needed if not setting values or extending a Class.
     */
    
public function __construct()
    {
        
parent::__construct();

        
log_message('debug'"Name Controller Class Initialized");
    }

    
/**
     * index ()
     * -----------------------------------------------------------------------
     *
     */
    
public function index()
    {
        
$data = [];
        
        
$this->load->view('Your_view'$data);

    }


    
// -----------------------------------------------------------------------

  // End of Home Controller Class.


/**
 * -----------------------------------------------------------------------
 * Filename: Home.php
 * Location: ./application/controllers/Home.php
 * -----------------------------------------------------------------------
 */ 

Hope that helps.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#5
@InsiteFX: My bad. Worked in CI 2, but apparently that where a bug so it got fixed.
Reply

#6
Right, they fixed users were abusing it in version 2.

I just make a MY_Controller that has a BaseController then I extented that into
a Backend and Frontend controller. Backend for Admin area and Frontend for the public area.

As for the admin I use an Admin directory under controllers and make views with directories.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#7
Replace your .htaccess code by this.

RewriteEngine On
RewriteBase /ci/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Replace base_url in config.php file
$config['base_url'] = 'http://127.0 0.1/ci/';
$config['index_page'] = '';

Use home controller code
<?php
/*
Welcome controller for front
*/
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends CI_Controller {

function __construct() {
Parent::__construct();
}

function index() {
       $this->load->view('front/frontend/home');
}
}

browser url:
http://127.0 0.1/ci/frontend/home


By:Xtreem Solution

[Highly Skilled Laravel Developer](https://xtreemsolution.com/hire-laravel-developer.html)

[Dedicated PHP Developer](https://xtreemsolution.com/hire-php-developer.html)
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.