CodeIgniter Forums
Problem setting routes in HMVC - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Installation & Setup (https://forum.codeigniter.com/forumdisplay.php?fid=9)
+--- Thread: Problem setting routes in HMVC (/showthread.php?tid=65324)

Pages: 1 2 3


Problem setting routes in HMVC - bebetxx - 05-30-2016

Hello guys,

I am quite new to CodeIgniter and MVCs. I started working on this new project and installed the HMVC from wiredesignz . I encounter a HMVC routing problem and this thing drives me CRAZY!

My problem is that my routing is not working properly. I write you the details of my CI3 project.

application/config
   config.php

PHP Code:
$config['base_url'] = 'http://localhost/Hub2/';

$config['index_page'] = '';
....

$config['modules_locations'] = array(
   APPPATH.'modules/' => '../modules/'
); 


   routes.php

PHP Code:
//$route['(:any)'] = 'pages/view/$1';
$route['login'] = 'authentication/login/index';
$route['awesome'] = 'pages/pages/index';
$route['default_controller'] = 'pages/pages/view';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE

   application/modules/authentication/controllers/Login.php
   application/modules/authentication/model/User.php
   application/modules/authentication/view/login_box.php

   application/modules/pages/controllers/Pages.php
   application/modules/pages/view/template/.......

my .htacess from the root directory

Code:
<IfModule mod_rewrite.c>
  RewriteEngine On
  # !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
  #  slashes.
  # If your page resides at
  #  http://www.example.com/mypage/test1
  # then use
  # RewriteBase /mypage/test1/
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

Here is my Login Class:


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


class 
Login extends MY_Controller
{

    function __construct()
    {
        parent::__construct();

        $this->sModuleName='authentication';
    }

    public function index()
    {
        echo "Login";
    

MY_Controller

PHP Code:
class MY_Controller extends MX_Controller
{

    public $data = array();
    public $sModuleName='';

    function __construct()
    {
        parent::__construct();
        $this->sModuleName get_class($this);

        $this->data['g_sWebsiteName'] = "SkyHUB";
        $this->data['g_sLanguage'] = "en";
    }

    function my_view($path,$data=NULL,$bDisplay=false)
    {
        return $this->load->view('../modules/'.$this->sModuleName.'/views/'.$path,$data,$bDisplay);
    }




When I can access the following links: 
http://localhost/Hub2/ returns me correctly the Modules/Pages/Controllers/Pages/View

http://localhost/Hub2/login returns me Not Found. The requested URL /Hub2/login was not found on this server. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

http://localhost/Hub2/awesome returns me Not Found. The requested URL /Hub2/awesome was not found on this server. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

http://localhost/Hub2/index.php/login returns me correctly the Modules/Authentication/Controllers/Login/Index
http://localhost/Hub2/index.php/awesome returns me correctly the Modules/Pages/Controllers/Pages/Index


RE: Problem setting routes in HMVC - InsiteFX - 05-30-2016

For one you are specifying the index method in your route, this is not needed due to the fact that the index method is called as the default method.

Your default controller is wrong - You can NOT use a directory as a part of this setting!


RE: Problem setting routes in HMVC - bebetxx - 05-30-2016

(05-30-2016, 04:02 AM)InsiteFX Wrote: For one you are specifying the index method in your route, this is not needed due to the fact that the index method is called as the default method.

Your default controller is wrong - You can NOT use a directory as a part of this setting!


Thank you for replying, I really appreciate it. Unfortunately I did not really understand your points. Can you point out the lines where I am doing wrong.

Best regards


RE: Problem setting routes in HMVC - InsiteFX - 05-30-2016

PHP Code:
// This only takes a method no directories are allowed
// so the default controller is under controllers
$route['default_controller'] = 'pages/pages/view';

$route['default_controller'] = 'pages';

--------------------------------------------------

// index is the default method that is called.
$route['login'] = 'authentication/login/index';

$route['login'] = 'authentication/login'

If you need like cms pages then I use this in the controller: 

PHP Code:
// See the URI Class library in the CodeIgniter Users Guide.
$page $this->uri->segment(30); 



RE: Problem setting routes in HMVC - bebetxx - 05-31-2016

(05-30-2016, 10:12 AM)InsiteFX Wrote:
PHP Code:
// This only takes a method no directories are allowed
// so the default controller is under controllers
$route['default_controller'] = 'pages/pages/view';

$route['default_controller'] = 'pages';

--------------------------------------------------

// index is the default method that is called.
$route['login'] = 'authentication/login/index';

$route['login'] = 'authentication/login'
I did the changes and I still have the same 404 message when I access http://localhost/Hub2/login Not Found. The requested URL /Hub2/login was not found on this server. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

When I access http://localhost/Hub2/index.php/login it works like before getting my login index method but definitely is not what I want.

What will be the difference with this new code from here?
[quote pid='332863' dateline='1464628329']
If you need like cms pages then I use this in the controller: 

PHP Code:
// See the URI Class library in the CodeIgniter Users Guide.
$page $this->uri->segment(30); 

[/quote]


RE: Problem setting routes in HMVC - InsiteFX - 05-31-2016

Try this .htaccess file, it's from the FuelPHP and works very well:

Code:
<IfModule mod_rewrite.c>

    #Set the CodeIgniter Environment.
    #SetEnv CI_ENV development

   # Make sure directory listing is disabled
    Options +FollowSymLinks -Indexes
    RewriteEngine on

    # NOTICE: If you get a 404 play with combinations of the following commented out lines
    #AllowOverride All
    #RewriteBase /wherever/ci/is
    RewriteBase /

    # Restrict your site to only one domain
    # Important USE ONLY ONE OF THESE OPTIONS BELOW!

    # Option 1: To rewrite "www.domain.com -> domain.com" uncomment the following lines.
    #RewriteCond %{HTTPS} !=on
    #RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    #RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

    # Option 2: To rewrite "domain.com -> www.domain.com" uncomment the following lines.
    #RewriteCond %{HTTPS} !=on
    #RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
    #RewriteCond %{HTTP_HOST} (.+)$ [NC]
    #RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]

    # Option 3: Remove index.php from URL
    #RewriteCond %{HTTP:X-Requested-With}    !^XMLHttpRequest$
    #RewriteCond %{THE_REQUEST}                ^[^/]*/index\.php [NC]
    #RewriteRule ^index\.php(.*)$            $1 [R=301,NS,L]

    # Send request via index.php (again, not if its a real file or folder)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    #RewriteCond $1 !^(index\.php|public_html|\.txt|robots\.txt|favicon\.ico|style\.css)

    # deal with php5-cgi first
    <IfModule mod_fcgid.c>
        RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
    </IfModule>

    <IfModule !mod_fcgid.c>

        # for normal Apache installations
        <IfModule mod_php5.c>
            RewriteRule ^(.*)$ index.php/$1 [QSA,L]
        </IfModule>

        # for Apache FCGI installations
        <IfModule !mod_php5.c>
            RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
        </IfModule>

    </IfModule>

</IfModule>

The code I showed you for the CMS Pages allow you to grab the segments by the number /1/2/3/4 etc;


RE: Problem setting routes in HMVC - bebetxx - 05-31-2016

(05-31-2016, 08:35 AM)InsiteFX Wrote: Try this .htaccess file, it's from the FuelPHP and works very well:

Code:
<IfModule mod_rewrite.c>

    #Set the CodeIgniter Environment.
    #SetEnv CI_ENV development

   # Make sure directory listing is disabled
    Options +FollowSymLinks -Indexes
    RewriteEngine on

    # NOTICE: If you get a 404 play with combinations of the following commented out lines
    #AllowOverride All
    #RewriteBase /wherever/ci/is
    RewriteBase /

    # Restrict your site to only one domain
    # Important USE ONLY ONE OF THESE OPTIONS BELOW!

    # Option 1: To rewrite "www.domain.com -> domain.com" uncomment the following lines.
    #RewriteCond %{HTTPS} !=on
    #RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    #RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

    # Option 2: To rewrite "domain.com -> www.domain.com" uncomment the following lines.
    #RewriteCond %{HTTPS} !=on
    #RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
    #RewriteCond %{HTTP_HOST} (.+)$ [NC]
    #RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]

    # Option 3: Remove index.php from URL
    #RewriteCond %{HTTP:X-Requested-With}    !^XMLHttpRequest$
    #RewriteCond %{THE_REQUEST}                ^[^/]*/index\.php [NC]
    #RewriteRule ^index\.php(.*)$            $1 [R=301,NS,L]

    # Send request via index.php (again, not if its a real file or folder)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    #RewriteCond $1 !^(index\.php|public_html|\.txt|robots\.txt|favicon\.ico|style\.css)

    # deal with php5-cgi first
    <IfModule mod_fcgid.c>
        RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
    </IfModule>

    <IfModule !mod_fcgid.c>

        # for normal Apache installations
        <IfModule mod_php5.c>
            RewriteRule ^(.*)$ index.php/$1 [QSA,L]
        </IfModule>

        # for Apache FCGI installations
        <IfModule !mod_php5.c>
            RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
        </IfModule>

    </IfModule>

</IfModule>

The code I showed you for the CMS Pages allow you to grab the segments by the number /1/2/3/4 etc;

I appreciate your input. 

I did the change, but it is still not working. When I try to access: http://localhost/Hub2/login I get a new error: Not Found. The requested URL /Hub2/login was not found on this server.

I can access the following addresses (like before) http://localhost/Hub2/ and http://localhost/Hub2/index.php/login


RE: Problem setting routes in HMVC - kilishan - 05-31-2016

It looks like you need to update your RewriteBase, also. Currently it will think your website root is in /localhost, I believe, when yours is actually in a subfolder. Try changing it's value in .htaccess to :

Code:
RewriteBase /Hub2



RE: Problem setting routes in HMVC - bebetxx - 05-31-2016

I tried many times, I hate it - it's not working(I get the same error)! Tomorrow I will upload the code on github and I'll give it to you guys.

LE: After I change the .htacess do I need to restart my apache?


RE: Problem setting routes in HMVC - InsiteFX - 05-31-2016

Any time you make changes it is aa good idea to restart everything.

Change your route.php to look like this one.

PHP Code:
// This route cannot have a directory name!
$route['default_controller'] = 'pages/pages/view';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

$route['login'  'authentication/login';
$route['awesome'] = 'pages/pages';

// Routes with '(:any)' must be the last routes they are a catch all route.
//$route['(:any)'] = 'pages/view/$1';