Few question on start

71 views
Skip to first unread message

malan

unread,
Apr 16, 2012, 9:52:54 AM4/16/12
to rainfr...@googlegroups.com
Hi,

I decided to use your framework in my new project - it's very light and easy to learn so I think that it's good choice :) Of course I read your documentation, but some thinks are still not clear for me.

1. I would like to use my own class to manage session and I don't know how to load it to "system". I wouldn't like to to use globals, but I want to use class object in models.
Example:
//boostrap.php
//...
$loader->auto_load_controller();
//...

//MyModel.php
class MyModel extends Controller
{
public function some_action()
{
// here i want to user session obect

$this->load_model('session', 'session'); // something like that?
$this->session->get_something();

//...
}
2. I'll use ACL. Where should I check if user has access to some actions?
class MyModel extends Controller
{
public function some_action()
{
$acl = new ACL($session->getUserId());
if( $acl->allowd('MyModel/someaction') )
{
echo 'hello!';
}else
{
show_info('access denied!');
}
//...
}
Maby I should do it before the system get in to the controller?

I know that those questions are simple, but I must be sure :)
Best regards :)

Federico Ulfo

unread,
Apr 18, 2012, 10:14:23 PM4/18/12
to rainfr...@googlegroups.com
Hi Malan,

sorry for the delayed answer. 

1. To use your model you have to change something in bootstrap.php and ajax_bootstrap.php:

-   $loader->load_session();
+  $loader->load_model( "YourSessionClass", "my_session" );

so now you can class your class with $loader->my_session->your_class_method(); 
If you are inside the controller or the model you can use $this->loader, so you can do call your model with $this->loader->my_session.

In alternative you can create Session as static or singleton class and load it with $loader->load_library("YourSessionClass");


2. ACL seams a good idea to me. The way to load it and use it is basically the same.


Thanks for choosing RainFramework, and please keep me updated with the progress of your project!

malan

unread,
Apr 19, 2012, 2:43:44 PM4/19/12
to rainfr...@googlegroups.com
Hi Rain!

Thank you very, very, very much for the answer! :)

1. Your solution doesn't work or I didn't understand you...
When I run your code I get error:

Notice: Undefined property: Loader::$my_session in \app\bootstrap.php ...
Fatal error: Call to a member function isGuest() on a non-object in \app\bootstrap.php on line 26 

[Loader::load_model] method doesn't have second argument so the only think I can do is:

bootstrap.php
$session = $loader->load_model('Session');
$session->myMethod();

Now it's working, but I can't use this object in controllers and models. In that situation I will have to use singleton.
2. I'm looking for a solution to integrate ACL with your framework without checking entitlements in every controller's method:
BAD solution:
class My_Montroller extends Controller
{
public function method1()
{
$acl->isAllowed(My_Controller/method1')
{
//...
}else
{
//...
}// if
}// method
}// class

It seems to me that I've got idea, but I don't know how to implement it yet :)

bootstrap.php
//...
if( ! $acl->allowed($_SERVER['PHP_SELF'] )
{
// Here I must "force" on current controller to execute method [Controller::filter_on_access_denied] which I added to YOUR base controller class:
/*
Controller
{
//...
public function filter_on_access_denied()
{
$tpl = new View;
$tpl->assign('access_denied_message' => 'Bye bye! ;)'
$tpl->draw('acl/denied');
}
//...
}
*/
}

$loader->draw();

With this solution I'll have full control on every action in my application without adding any code. [acl/denied.html] template will be draw in the controller's load_area so user will see the announcement "in" page layout.
Any idea how to implement my "epic" solution? Is it generally possible? :)

Rain

unread,
Apr 19, 2012, 3:00:47 PM4/19/12
to rainfr...@googlegroups.com
Hey,
I'll check it again and give you a working example for the Session. 

Well, there are many ways to make ACL visible from Controllers and Models, the simplest is by using Static class or Singleton class, another solution is to inject the object in all controllers/models. I personally prefer to use Static class because Rain Framework 2 is intended to create small project, easy and fast, easy to read and fast because there are just a few layers. Probably in the next version we'll introduce a stronger structure, as we are doing in Rain Tpl 3, but is important to us to keep it easy to use.

Talk to you soon
Reply all
Reply to author
Forward
0 new messages