ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Modular Extensions - HMVC version 5.4 How to prevent direct Module Method access

July 30, 2012 7:31pm

Subscribe [5]
  • #1 / Jul 30, 2012 7:31pm

    spidersbite

    1 posts

    Hi, I am a Newbie and have been using CI for a while, and have just started with HMVC. I have modified the files etc as in the Modular Extensions 5.4 (Bitbucket) and it works fine.

    My question is?

    I have just set up 2 modules Module: Base (originally the welcome one) and another Module: Register.

    I am experimenting with running the Module register from within the base Module/view file thus:

    <div id="container">
    <h1>Welcome to CodeIgniter!</h1>
    <div id="body">
    The page you are looking at is being generated dynamically by CodeIgniter.
    If you would like to edit this page youll find it located at:
    <code>application/views/welcome_message.php
    The corresponding controller for this page is found at: application/controllers/welcome.php If you are exploring CodeIgniter for the very first time, you should start by reading the User Guide. </div> Page rendered in {elapsed_time} seconds ?php echo Modules::run('register/register/index'); ?> </div></code>

    As I understand it this is a view partial?

    Which I can access with the url: http://localhost/CI-HMVC/index.php/ and it loads the base module fine and also the Register Module within it.

    Now if I enter http://localhost/CI-HMVC/index.php/register I can also access the Register Module directly on its own, which of course looks bad as it is not loading any of the CSS etc of the base view.

    Is there a way to prevent direct access to a Module Method unless it it is called/run from another Module, in this case ‘BASE’?

    Here is the simple Register Module:

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Register extends CI_Controller {
    
     /**
      
      */
     public function index()
     {
      $this->load->view('register');
     }
    }

    Many thanks, Alan

  • #2 / Jul 30, 2012 8:03pm

    PhilTem

    872 posts

    I think what you’re looking for is some solution like this (though I cannot confirm this is supported by Modules::run - but I’m guessing it should be 😉 )

  • #3 / Jul 31, 2012 1:07am

    wiredesignz

    2882 posts

    @spidersbite, Add a _remap() method to your module controller and use it to display an error.

    This method is explained in the Bitbucket wiki.

  • #4 / Jul 31, 2012 5:00am

    spidersbite

    1 posts

    @PhilTom and @wiredesignz many thanks for the advice, it was early hours in the uk so my brain was a bit fried.

    I did try private function _ etc but it didn’t appear to solve the issue, I am assuming that Modules::run does not support it.

    However just tried @PhilTom’s suggestion using _remap() and it appears to work, here is the modified module code:

    function _remap()
    {
          echo 'No direct access allowed';
    }
    
    public function index()
    {
          $this->load->view('register_form');
    }

    So if I try now to access the module via URL: http://localhost/CI-HMVC/index.php/register_form
    I get a warning message ‘No direct access allowed’.

    But loading the module via the initial Controller/view:

    <?php echo Modules::run ('register_form/register_form/index'); ?>

    loads the module correctly.

    Even trying to load the module via URL: http://localhost/CI-HMVC/index.php/register_form/index and I still get the warning ‘No direct access allowed’.

    So in my view _remap() Works a Treat!! 😊

    Many thanks again for the advice/info, I know my example was just a very simple one, but I was just getting started with HMVC and modules. (old age 😊 )

    Alan

  • #5 / Aug 09, 2012 10:50am

    Lotus

    5 posts

    Thank you!  This helped me 😊

  • #6 / May 25, 2013 6:22am

    reomind

    1 posts

    you rock. i wanted to secure my widgets, finally got the solution.
    Thanks

  • #7 / May 20, 2014 12:11pm

    rahendz

    13 posts

    first, sorry for my bad english. i just wanna simple ask, i use codeigniter v3.0 and implement the HMVC to it. it works well first when run the welcome module. but when i start using session it return error, i know there is a difference. the old one, session was load using library and in version 3 it load using driver. my question is, when hmvc will support CI 3? or there is something need to be done, to make it works well, some hacks maybe. thanks before.

  • #8 / May 20, 2014 12:22pm

    ivantcholakov

    251 posts

    @rahendz

    https://github.com/ivantcholakov/starter-public-edition-3

    I did the adaptation some time ago, it is tricky. For saving your time look at my source.

  • #9 / May 20, 2014 2:04pm

    rahendz

    13 posts

    @rahendz

    https://github.com/ivantcholakov/starter-public-edition-3

    I did the adaptation some time ago, it is tricky. For saving your time look at my source.

    @ivantcholakov

    already try it, even i tried the 4th edition. would you give me some documentation or video how to start new hello word from the begining? i really confused about the directory lol but thanks for replied

  • #10 / May 20, 2014 2:31pm

    ivantcholakov

    251 posts

    I have no time such things. Here is a hint for version 3:

    See platform/application/modules/welcome/views/welcome_message.php

    Remove my stuff there and put there echo ‘Hello world’;

    platform/application/modules/welcome/controllers/Welcome.php - remove in the index method my stuff about diagnostics.

    And you will have a clean starting page.

    Remove the demo directory platform/application/modules/playground/ - you would not need it.
    The other modules might be needed at your will.

    Remove the directory www/non-mvc/ - it is a demo too.

    The HTML layout (the master HTML template) for the pages is within platform/application/views/layouts/site_example.php - you may modify it depending on what css and javascript you intend to use, you may place HTML tags there that is common for all the pages. The Phill Sturgeon’s Template library is used for this functionality.

    And your knowledge from CI 2.x projects is applicable for this platform too. I tried to keep backward compatibility as much as it is possible. You may try to use your CI 2.x code, libraries etc. with no or very little modifications.

    If you pick one of the platforms and you feel some discomfort - open an issue at GitHub on specific things, I will reply.

    Edit: The other two options are:

    2. You to wait when an official HMVC for CodeIgniter would be released - this I don’ know;
    3. To analyze my implementation of HMVC and to port it alone within your project - I don’t know whether you can do this.

  • #11 / May 20, 2014 7:43pm

    InsiteFX

    6819 posts

    Wiredesignz, will update the HMVC when they release the CI 3.0

  • #12 / May 21, 2014 2:45am

    rahendz

    13 posts

    I have no time such things. Here is a hint for version 3:

    See platform/application/modules/welcome/views/welcome_message.php

    Remove my stuff there and put there echo ‘Hello world’;

    platform/application/modules/welcome/controllers/Welcome.php - remove in the index method my stuff about diagnostics.

    And you will have a clean starting page.

    Remove the demo directory platform/application/modules/playground/ - you would not need it.
    The other modules might be needed at your will.

    Remove the directory www/non-mvc/ - it is a demo too.

    The HTML layout (the master HTML template) for the pages is within platform/application/views/layouts/site_example.php - you may modify it depending on what css and javascript you intend to use, you may place HTML tags there that is common for all the pages. The Phill Sturgeon’s Template library is used for this functionality.

    And your knowledge from CI 2.x projects is applicable for this platform too. I tried to keep backward compatibility as much as it is possible. You may try to use your CI 2.x code, libraries etc. with no or very little modifications.

    If you pick one of the platforms and you feel some discomfort - open an issue at GitHub on specific things, I will reply.

    Edit: The other two options are:

    2. You to wait when an official HMVC for CodeIgniter would be released - this I don’ know;
    3. To analyze may implementation of HMVC and to port it alone within your project - I don’t know whether you can do this.

    thanks for your reply and support, i’ll try it and come back soon with feedback. thanks again btw.

  • #13 / May 21, 2014 2:47am

    rahendz

    13 posts

    Wiredesignz, will update the HMVC when they release the CI 3.0

    there is a branch release for codeigniter on github, is that not the official yet? it’s v3.0 right?

  • #14 / May 21, 2014 1:06pm

    ivantcholakov

    251 posts

    Wiredesignz, will update the HMVC when they release the CI 3.0

    there is a branch release for codeigniter on github, is that not the official yet? it’s v3.0 right?

    At this moment narfbg has merged everything from develop branch with release/3.0 branch. I suppose, the release/3.0 branch has been opened for convenience - code is updated there with some delay in comparison to develop branch.

    There is no an official release tag however.

    I suppose, from your perspective it is more safe to start your project from release/3.0 branch, and to synchronize your code with it not so often. Thus, you will be protected from some accidental and inevitable mistakes (typos mostly) that appear within the develop branch.

    @narfbg (if you see this) Do I guess it right?

  • #15 / May 21, 2014 3:12pm

    InsiteFX

    6819 posts

    He will not update it until it is released to the public.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases