cakephp accessing view attributes/variables from within a helper - php

is there a reasonable way to access the view attribute "passedArgs" (or any similar)
/* view */
$this->passedArgs
from within a Helper?
I'd be happy to customize the _construct() of the helper or to customize the app_helper... but I don't want to have to pass $this->passedArgs into the helper on every view or usage.

Cake 2.x and 3.x
You can look up your variables in the _View object:
$this->_View->viewVars['foo'];
Cake 1.x
If you grab the current view object from within the helper you should be able to get to its passedArgs.
class SomeHelper extends AppHelper {
function __construct($settings = array()){
$this->passedArgs = ClassRegistry::getObject('view')->passedArgs;
}
}
Cake 1.2.x
If you grab the current view object from within the helper you should be able to get to its viewVars.
class SomeHelper extends AppHelper {
function __construct($settings = array()){
$this->viewVars = ClassRegistry::getObject('view')->viewVars;
}
}
Enjoy,
Nick

Have you tried just setting the view's value from the AppController?
class AppController extends Controller {
function beforeFilter() {
// other stuff
$this->set( 'passed_args', $this->params['pass'] );
}
}

Cake 3:
$this->getView()->get('my_var');

Related

Use phalcon classes in base controller rather than on individual controllers

I have a base controller as follows
<?php
use Phalcon\Mvc\Controller;
class ControllerBase extends Controller {
public function initialize() {
}
// wrapper function for debug purposes.
public function pr($data = null) {
echo '<pre>';
print_r($data);
echo '</pre>';
}
}
and a users controller as follows
<?php
use Phalcon\Mvc\Model\Criteria;
use Phalcon\Paginator\Adapter\Model as Paginator;
use Phalcon\Mvc\View;
class UsersController extends ControllerBase {
public function initialize() {
// initialize parent, here ControllerBase.
parent::initialize();
}
public function loginAction() {
// disable the main layout.
$this->view->disableLevel(View::LEVEL_MAIN_LAYOUT);
// disable the controller layout.
$this->view->disableLevel(View::LEVEL_LAYOUT);
}
.
.
.
.
other functions...
}
i was wondering if i could call all the required phalcon classes in base controller and extend then to all the child classes so that i dont need to call them individually on each controller.
in otherwords, can i add the below code
use Phalcon\Mvc\Model\Criteria;
use Phalcon\Paginator\Adapter\Model as Paginator;
use Phalcon\Mvc\View;
only in the base controller and acces them in other controllers. I tried putting them base controller but it gave error : Class not found.
Is this the right way or is there something wrong in my approach...please help.
If I understand your question correctly the answer is NO.
Namespaces are language feature and works this way. The use Phalcon\Mvc\Model\Criteria only declares that you'll use Criteria class from Phalcon\Mvc\Model\ namespace. So in your code you can write new Criteria() to create object instead of using its' full name new \Phalcon\Mvc\Model\Criteria().
You must declare each class in every file which instantiates object of that class so autoloader will know in which file given class exists.

laravel 4 - How to load different layout for a particular method?

I have something like this :
class AController extends BaseController
{
protected $layout = "templates.layouts.master";
protected $data = "something";
public function alt()
{
// this is wrong
// i want to override "templates.layouts.master"
// missing something obviously here
$this->layout = ??? what should do?
$this->layout->content = View::make("content", $this->data);
}
}
In method alt, I wish to use different layout than the default "templates.layouts.master".
I have very limited laravel 4 knowledge. This maybe something easy to achieve, but is beyond my knowledge.
Possible solutions that I forsee:
define a construct method, and detect what is the current method, and set a different value for $layout (However, I not sure how to get the current method name).
do an assignment like what I put in above.
Which is the correct way?
You can set the layout to be another view per method:
class AController extends BaseController
{
protected $layout = "templates.layouts.master";
protected $data = "something";
public function alt()
{
$this->layout = View::make('templates.layouts.alt');
$this->layout->content = View::make("content", $this->data);
}
}
If you check out the BaseController, you'll see that all it does is call View::make() to set the layout view. You can do the same to over-ride its default.
OK, solution 1 seems to be possible, but I think is fugly :
class AController extends BaseController
{
public function __construct()
{
if (Request::is("..."))
{
$this->layout = "alternative layout";
}
}
}

Codeigniter -fetching data sitewide with codeigniter hooks

I am trying to get scroll news in any page of the website from database without having to pass the variable in every controller of the site. So I decided to use hooks instead of passing scroll variable in every controller.
I did create a class like this
class Scroll
{
function getScroller()
{
$data = array();
$CI =& get_instance();
$CI->db->where('a_status','active');
$CI->db->limit(4);
$CI->db->order_by('id','desc');
$Q = $CI->db->get('news');
if($Q->num_rows() > 0){
foreach($Q->result_array() as $row){
$data[] = $row;
}
}
$Q->free_result();
return $data;
}
}
What I get now is
Severity: Notice
Message: Trying to get property of non-object
Call to a member function get() on a non-object in E:\xampp\htdocs\
Can anyone please help me how to do this ? Thanks I want to get scrollernews in any controller's view automatically without having to pass in each controller. Thanks
If you are defining that on a view level, there is no need for that.
You can define db requests directly in a view.
Other approach would be to have separated controller which with separate view and load it in the page through iframe. It's often used for "web widgets" that can be later on loaded in to another pages.
Extending the core class of CI Controller will should cause you less troubles.
http://ellislab.com/codeigniter/user-guide/general/core_classes.html
application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
//By do this, all controllers who use this class as parent controller
//will have $news in their views
$this->load->vars(array(
'news' => array()
));
}
}
application/controller/welcome.php
class Welcome extends MY_Controller {
public function index()
{
$this->load->view('welcome_message');
}
}
application/views/welcome_message.php
var_dump($news);
use separated library or helper and call that method on controller's constructs like :
class My_Controller extends CI_Controller(){
function __construct(){
parent::__construct();
//load your library
//call your library method
}
}

Same data variable passing to view

In my project, I have one search section with 3 select box. I passed the value to it using
$data['restaurant_all']=$this->restaurant_model->get_all('','','','','','yes')->result();
$data['state_all']=$this->state_model->get_all();
$data['menu_all']=$this->menu_model->get_all('all','','','','','','yes')->result();
$data['restaurant']=$this->input->post('restaurant');
$data['state']=$this->input->post('area');
$data['food_type']=$this->input->post('menu');
I need this statement in all my pages. In there any way to accomplish this without writing these statements in all the pages
a. extend the default controller by creating a file MY_Contoller.php at a suitable location.
b. create a custom class that will extend the default controller.
c. add a protected or public variable $data to custom class.
e. do something with data using __construct()
d. make every controller extend the custom controller.
e. you can access this variable like any other class variable.
example code:
MY_Controller.php
class APP extends CI_controller {
protected $data;
function __construct() {
parent::__construct();
$this->_init();
}
function _init() {
$this->data['state'] = $this->input->post('area');
}
}
normal controllers:
class Welcome extends APP {
function __construct() {
parent::__construct();
}
function view() {
/* pass this data value like normal data param */
$this->load->view('some_view', $this->data);
}
}
hope it helps.
Use constants, in /config/constants.php

Get request info in view helper

Is it possible in Zend View helper (extends Zend_View_Helper_Abstract) get info about module/controller/action in which that helper was called ?
Yes. You can use Zend_Controller_Front::getInstance() within view helpers. So you could do something like this:
class App_Helper_DoSomething extends Zend_View_Helper_Abstract
{
public function doSomething()
{
return Zend_Controller_Front::getInstance()
->getRequest()
->getControllerName();
}
}
Which will print the current controller name when called in your view with:
echo $this->doSomething();

Categories