Welcome Guest, Not a member yet? Register   Sign In
How to extend core classes when working with the HMVC Extension?
#11

[eluser]Twisted1919[/eluser]
You are close but not there yet.
New things for you:
I am using MY_Controller.php like:
Code:
// location: application/core/MY_Controller.php
/* The MX_Controller class is autoloaded as required */
class MY_Controller extends MX_Controller{

   public function __construct(){
      parent::__construct();  
      //[do stuff here if needed]
   }

}
Also, i have a Admin_Controller.php and a Public_Controller.php
Code:
// location: application/libraries/Admin_Controller.php
class Admin_Controller extends MY_Controller{

    public function __construct()
    {
        parent::__construct();
        if(user is not logged in)
          redirect_to_login_page();
    }
}

// location: application/libraries/Public_Controller.php
class Public_Controller extends MY_Controller{

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

As you see, i have a base controller, MY_Controller, in where i can load/calculate things needed for both, frontend and backend.
Then, i have two other controllers, Admin_Controller which loads things needed for admin panel And Public_Controller which loads things needed for fronted only.
Each controller from modules that handles admin tasks, will extend the admin controller and each controller that handles the fronted will extend the Public_Controller.


The interesting thing is the Admin_Controller, which checks if the user is allowed to access the admin panel,
and if it is not we redirect to the login page.
Any controller extending Admin_Controller will have the functionality of the admin controller, therefore, in those controllers we don't need to check if the user is logged in or not, because this is already done by the parent controller (Admin_Controller).

So all your admin controllers will extend Admin_Controller and the others will extend Public_Controller.
Similar to this, in case you have a area in fronted which is allowed to be accessed only by logged in users, you can simply create another User_Controller.php in where you check if the user is logged in, then extend all the controllers from that module (except the admin) from the User_Controller instead of Public_Controller.

I know that there is allot of information to process, but it should give you a starting point, a very good starting point. Also, it will help you allot if you understand PHP OOP already.
#12

[eluser]gunnarflax[/eluser]
I have a very good grasp of PHP OOP and have been working with it for quite a while, it's just the HMVC pattern which confuses me Smile The thing with the method you mentioned above is that all the modules would have to extend the Admin_Controller instead of the MX_Controller and that would break portability between applications. Or am I driving to hard on the portability issue?
#13

[eluser]gunnarflax[/eluser]
And what if I would want a permission module which checks if the user is authorized to perform an action within a controller, like edit other's pages - which he's not, but he's allowed to edit his own pages? Those two methods would be probably be within the same controller but how can you fix that? The more I think of it the more problems I see with the HMVC pattern, but it's goal is without a doubt something I want to achieve but how have these issues been solved by other people? Twisted1919 have you ever implemented a permission system into a HMVC project and if so how did you do it?
#14

[eluser]Twisted1919[/eluser]
Quote:The thing with the method you mentioned above is that all the modules would have to extend the Admin_Controller instead of the MX_Controller and that would break portability between applications. Or am I driving to hard on the portability issue?
From my own experience, i found this way to be very useful, this is the way i do it and i would suggest you do it the same.

Quote:And what if I would want a permission module which checks if the user is authorized to perform an action within a controller, like edit other’s pages - which he’s not, but he’s allowed to edit his own pages? Those two methods would be probably be within the same controller but how can you fix that? The more I think of it the more problems I see with the HMVC pattern, but it’s goal is without a doubt something I want to achieve but how have these issues been solved by other people? Twisted1919 have you ever implemented a permission system into a HMVC project and if so how did you do it?
In this case, just create a library in application/libraries which will handle these kind of stuff, load the library on the app init so that you can access it in your modules like $this->library->do_some_checks();

Think like this, use HMVC functionality to structure your models and controllers. For everything that is shared between the modules, use libraries.

And because you asked, this is what i am doing. My "modules" only have controllers/views/models (and some of them, libraries, but only if the library is needed only by that module).
I have a "User" library which handles all the things related to group/permission/login/logout of the user, and when i need to check something related to a user, i just call $this->user->some_check().

You might be confused by the module notion, which for example on drupal is something totally different.
In drupal modules do same thing like wordpress plugins, they offer extra functionality for a base system.
HMVC cannot be treated like this, because the purpose of HMVC is totally different.
#15

[eluser]gunnarflax[/eluser]
[quote author="Twisted1919" date="1313512378"]
You might be confused by the module notion, which for example on drupal is something totally different.
In drupal modules do same thing like wordpress plugins, they offer extra functionality for a base system.
HMVC cannot be treated like this, because the purpose of HMVC is totally different.[/quote]

Ok, thanks, because I was thinking of them more like standalone packages which just could be put into any application without any changes made. I guess this was what made it harder to grasp.

This might be a little off topic but since HMVC is implemented in CodeIgniter through an extension I went searching for an alternative to CI and found Kohana, which is a CodeIgniter fork that supports HMVC out of the box. Can anyone recommend this framework?
#16

[eluser]Twisted1919[/eluser]
If you are not tide up with CodeIgniter, meaning you don't have any live projects created with CI, then give Kohana a try. I've used it and it's a pretty simple nice framework with all the things you'd want CI to have.
I am using Yii now, in my new projects and still maintain all the projects created with CI.
You could give Yii a try, it's more advanced than these two, and harder to understand, but in the end it pays off.
#17

[eluser]gunnarflax[/eluser]
For my current project I will probably stick with CI, but in the future I will probably use Kohana because of their excellent module features. I haven't heard very much about Yii, but maybe I'll look into that one as well Smile




Theme © iAndrew 2016 - Forum software by © MyBB