Re: Assigning, accessing and comparing viewVars in a controller...
On Mon, Aug 10, 2009 at 1:24 PM, gestejim.ho...@gmail.com wrote: Brian, Many thanks. I got over a bit of a hump on this on Friday with your help. I do of course have another n00b follow up question :) I am using the authake plug-in for group-based authorization (this nice plug-in has been taken over by Marco Sbragi and is on CakeForge: http://cakeforge.org/projects/authake2/ I has a number of functions such as getLogin() and isPermitted() that seem to work in views. For example, I can user getLogin() to display user ID on the menu page with echo $authak3-getLogin() However, if I try to access the same function in my menus_controller.php with: $testuser = $authak3-getLogin(); I then get a completely empty page/view. I am trying to determine if there is some sort of a general rule about scope of functions between controller and view that makes the whole thing bomb if I reference it from controller. One thing I am guessing is relevant is that Authake is referenced in the app_controller.php in a BeforeFilter() like so: $this-Authake-beforeFilter($this); That's because the View knows about $authak3, the Helper, but AppController has a handle on AuthakeComponent. I've never used the plugin but I had a look at it just now. You can access the same data in your controllers with $this-Authake-getLogin(), etc. Have a look through controllers/components/authake.php for the other stuff that it exposes. Compare that to plugins/views/helpers/authak3.php and all links/controllers are checked against Authake right from the start. The trick is that I want to check all possible menu links for access right *before* I add the link to viewVars-Menu for the view to render. I could access this authake3-isPermitted($menulink) in the view, but then I would wind up adding a pretty fair amount of logic like menu table queries to the view. I'm trying to make as much of that work in the controller as I can. It looks like you can get that from the component with $this-Authake-isAllowed(...). So any, broad clue about this appreciated. My question was starting to get somewhat Authake-specific, but I think I am probably missing something very general, very basic. Some of it's basic--keeping components and helpers straight (and I still get mixed up)--but having a concrete plugin (mini-app) as an example is actually pretty helpful. And the Authake-specific stuff is actually pretty important. I recently created a similar app using ACL, where the menu changes drastically between groups. Make sure to look into view caching so your controller can skip a lot of querying. You can name each view for the group_id, for instance. This makes it easy to delete when something Group-specific changes. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Assigning, accessing and comparing viewVars in a controller...
Brian, Many thanks. I got over a bit of a hump on this on Friday with your help. I do of course have another n00b follow up question :) I am using the authake plug-in for group-based authorization (this nice plug-in has been taken over by Marco Sbragi and is on CakeForge: http://cakeforge.org/projects/authake2/ I has a number of functions such as getLogin() and isPermitted() that seem to work in views. For example, I can user getLogin() to display user ID on the menu page with echo $authak3-getLogin() However, if I try to access the same function in my menus_controller.php with: $testuser = $authak3-getLogin(); I then get a completely empty page/view. I am trying to determine if there is some sort of a general rule about scope of functions between controller and view that makes the whole thing bomb if I reference it from controller. One thing I am guessing is relevant is that Authake is referenced in the app_controller.php in a BeforeFilter() like so: $this-Authake-beforeFilter($this); and all links/controllers are checked against Authake right from the start. The trick is that I want to check all possible menu links for access right *before* I add the link to viewVars-Menu for the view to render. I could access this authake3-isPermitted($menulink) in the view, but then I would wind up adding a pretty fair amount of logic like menu table queries to the view. I'm trying to make as much of that work in the controller as I can. So any, broad clue about this appreciated. My question was starting to get somewhat Authake-specific, but I think I am probably missing something very general, very basic. Jim On Aug 7, 6:49 pm, brian bally.z...@gmail.com wrote: Controller::set(), as you can see, adds a variable with the supplied name value to the $viewVars array which, in turn, is made available to the View object. However, you're not creating a *local* variable with the given name value. For that, you'd need to first asign it to a variable, then use set() to make it available to the View: $foo = 'bar'; $this-set('foo', $foo); If you need to set several vars for the view but need to use them in the controller, you can do: $foo = 'foo'; $bar = 'bar'; ... $this-set(compact('foo', 'bar')); ... which will do the same thing. One other thing you should know about: Cake will screw with your variable names if you're not careful. If there's an underscore in the variable name and you use compact(), you have to take an extra measure or Cake will create a camel-cased variable name (no, I don't understand why). So, if you do: $this-set(compact('foo_bar')); ... you'll end up with a $fooBar variable in the View instead of $foo_bar. To avoid this, do: $foo_bar = 'whatever'; $this-set(compact('foo_bar'), false); On Fri, Aug 7, 2009 at 6:03 PM, gestejim.ho...@gmail.com wrote: Sometimes I hit myself with a small, embarrassing clue stick immediately after posting. I thought I had tried using $this-viewVars['whatever' ] in my find conditions and that it just didn't work. But I just tried that little test of: $this-set('menu_link', /budgets ); $this-set('menu_name', $this-viewVars['menu_link']); and it does seem to work. (menu_name gets set to /budgets) So I think I need to go back and rework my find() conditions to try $this-viewVars[whatever] again Any other comments about the validity of using $this-viewVars in controller logic welcome. Jim --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Assigning, accessing and comparing viewVars in a controller...
Background: i am trying to build a little database-driven 2-level menu system that checks authorization to access menu pages and actions *before* they are displayed. If you are not authorized to use the budgets table in any way, you won't be presented with a budgets menus option (I am using Authake for the auth function) To stick with MVC convention, I figured I should put as much of the DB access and auth in my menus controller. I would use find() with some complex conditions to extract only the set of menus that I wanted a user to get. I had problems with my conditions in find() and I concluded I didn't understand it well enough -- maybe a fundamental misunderstanding about variable scope and what could be sent in to a query via find(). So I backed up and added echo pre; print_r($this-viewVars);echo /pre;? to my default.ctp so I could see what vars were being set in my controller: I am using routes and using URL comparison to select menu records from the database. I can set this in the controller: $this-set('current_uri', $_SERVER[REQUEST_URI]); The idea is to use URL to set parent menu and then query all parent +child menu records. But when I went to use $current_uri it in a find(all, [conditions]) the find simply failed So I did another test. I can set: $this-set('menu_link', /budgets ); but when I subsequently try to do this set in the next line in the controller: $this-set('menu_name', $menu_link ); I see menu_name in viewVars, but it has no value. I get the same no- value result if I use: $this-set('menu_name', $this-menu_link ); All this tells me that there is something really basic this perpetual n00b doesn't understand about scope of vars in viewVars and/or how they can be compared or set in the controller. Any thumps with the clue stick appreciated. Jim --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Assigning, accessing and comparing viewVars in a controller...
Sometimes I hit myself with a small, embarrassing clue stick immediately after posting. I thought I had tried using $this-viewVars['whatever' ] in my find conditions and that it just didn't work. But I just tried that little test of: $this-set('menu_link', /budgets ); $this-set('menu_name', $this-viewVars['menu_link']); and it does seem to work. (menu_name gets set to /budgets) So I think I need to go back and rework my find() conditions to try $this-viewVars[whatever] again Any other comments about the validity of using $this-viewVars in controller logic welcome. Jim --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Assigning, accessing and comparing viewVars in a controller...
Controller::set(), as you can see, adds a variable with the supplied name value to the $viewVars array which, in turn, is made available to the View object. However, you're not creating a *local* variable with the given name value. For that, you'd need to first asign it to a variable, then use set() to make it available to the View: $foo = 'bar'; $this-set('foo', $foo); If you need to set several vars for the view but need to use them in the controller, you can do: $foo = 'foo'; $bar = 'bar'; ... $this-set(compact('foo', 'bar')); ... which will do the same thing. One other thing you should know about: Cake will screw with your variable names if you're not careful. If there's an underscore in the variable name and you use compact(), you have to take an extra measure or Cake will create a camel-cased variable name (no, I don't understand why). So, if you do: $this-set(compact('foo_bar')); ... you'll end up with a $fooBar variable in the View instead of $foo_bar. To avoid this, do: $foo_bar = 'whatever'; $this-set(compact('foo_bar'), false); On Fri, Aug 7, 2009 at 6:03 PM, gestejim.ho...@gmail.com wrote: Sometimes I hit myself with a small, embarrassing clue stick immediately after posting. I thought I had tried using $this-viewVars['whatever' ] in my find conditions and that it just didn't work. But I just tried that little test of: $this-set('menu_link', /budgets ); $this-set('menu_name', $this-viewVars['menu_link']); and it does seem to work. (menu_name gets set to /budgets) So I think I need to go back and rework my find() conditions to try $this-viewVars[whatever] again Any other comments about the validity of using $this-viewVars in controller logic welcome. Jim --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---