Html Helper CakePHP - php

Fatal error:
Call to a member function charset() on a non-object in D:\xampp\htdocs\demo\app\controllers\test_controller.php on line 10
PHP Controller Code:
<?php
class TestsController extends AppController
{
var $name="Tests";
var $helpers = array('Html');
var $uses=array();
# demo action to check wheather html helper is working or not
function index()
{ echo "111111111";
echo $this->Html->charset();
echo "22222222222";
}
}
?>
I am getting the above error while hitting the url: http://localhost/demo/tests
I am using CakePHP 2.0 ALPHA (latest version).
Please let me know what is the root cause.

Following CakePHP's MVC convention, you should by using behaviors in models, components in controllers and helpers in views. You are currently trying to use a helper in a controller, which won't work. I suggest you go back and have another look at the documentation, but for something like HtmlHelper::charset() you really want to be calling that once in the <head> tag of your layout (which is also part of the view layer):
http://book.cakephp.org/2.0/en/views.html?highlight=layout#layouts

Related

Error: ControllerController could not be found

i just created a controller like this:
<?php
class UsuarioController extends AppController {
}
?>
I went to http://urubu.zz.mu/controller/UsuarioController.php to test, and this was the result:
Error: ControllerController could not be found.
Error: Create the class ControllerController below in file: app/Controller/ControllerController.php
}
Theres no class ControllerController in my project.
I found a line in AppController(From CakePHP) :
App::uses('Controller', 'Controller');
But i dont know what it means and how to solve it.
Can anyone help? thanks
TLDR:
Read the wealth of documentation in the Online CakePHP Book to better understand CakePHP.
Accessing a Controller's index() action
In CakePHP, you access the index action of your Controller via the URL like this:
http://www.mydomain.com/usarios
You do NOT need to add "controller" to the URL.
The above would run the index() action within the UsariosController.
Accessing another action within a Controller
http://www.mydomain.com/usarios/register
This would run the register() action within the UsariosController.
Passing variables to actions within a Controller
http://www.mydomain.com/usarios/view/1
This would pass 1 to the register($userId) { action in your UsariosController.

loading contents of PHP file in MVC architecture

So I'm using Codeigniter and I was wondering how I can load in the contents of my view directly into a calling JS file.
I have a view called "form_layout" which contains PHP code to populate form fields.
$('#wizard').smartWizard
({contentURL:'views/form_layout.php?action=1',
transitionEffect:'slideleft',onFinish:onFinishCallback});
but when I do that I get a server 500 error.
Do i have to route through the controller like?
<?php
class Form_manager extends CI_Controller {
public function index()
{
$this->load->view('form_template');
}
}
?>
and do contentURL:Form_manager/index?
Try This
var SITEURL = 'yourdomainname'; //like www.mysite.com
$('#wizard').smartWizard({
contentURL: SITEURL + '/Form_manager/index/action/1',
transitionEffect:'slideleft',onFinish:onFinishCallback
});
<?php
class Form_manager extends CI_Controller {
public function index($action )
{
echo $this->load->view('form_template','',true);
}
}
?>
Yes, you do have to route through a controller. CodeIgniter does not allow you to short-circuit routes to views directly (unlike Laravel). So, you will have to set up the controller as you show and change the url in your ajax call to site_url('form_manager'). You don't need to specify /index since that is the default function that CI calls when none is specified in the URL.

Kohana URL (segmented)

On Kohana 2.3.4 I use the query string for parameters and use the input class to get the values.
localhost/home?id=1234
Now that I migrated to Kohana 3.3.0, I am having a lot of trouble with how the segmented URL works.
I want to achieve this:
localhost/home/1234
My Controller Home:
class Controller_Home extends Controller_Template {
public function action_index($id) {
//code goes here
}
}
From my understanding of segmented URLs, my controller should have something like this:
public function action_details($id) {
//code here
}
But this would mean that the link is localhost/home/details/1234. I would prefer not to have the details action anymore. How can I do it? Sorry, at the moment I wrote this, Kohana seems to be down.
Since 3.3, you cant use params in actions. All you need is &$this->request->param('id'). See http://kohanaframework.org/3.3/guide/kohana/routing#request-parameters

Zend framework, View

Small mystery: can't seem to pass views between my Index controller (chartAction) and my view. When I go to my localhost it is not accessing the view phtml- instead it is just showing the controller every time (i.e: if I write "echo "HELLO WORLD!""; in my controller I get that echoed...but if I do a $this->view->test = "Hello World!" then access the index.phtml and type in echo $this->test; I get nothing (it still defaults to the controller action). Is there a step that I'm missing here? Why is my $this->view not functioning? I used the command line to create the view so I'm pretty sure that should be set up correctly.
Do I need to register something? Thanks for any help!
Assuming a standard MVC setup of ZF1.x, there is a definite relationship between the url, the controller and the action.
The url http://mydomain.com/index would call the index action of the index controller, typically the index action is the default action and is called automatically. the view script would be /application/views/scripts/index/index.phtml
The url http://mydomain.com/index/chart would call the chart action of the index controller and the view script would be /application/views/scripts/index/chart.phtml
Keep in mind that this behavior is changeable based on configuration and routing options.
It sounds like you may be fairly new at working with ZF. So something like the following may help demonstrate the relationship :
// application/controllers/IndexController.php
class IndexController extends Zend_Controller_Action
{
public function init()
{
}
public function indexAction()
{
$this->view->test = "Hello World, from the indexAction().";
}
public function chartAction()
{
$this->view->test = "Hello World, from the chartAction().";
}
// application/views/scripts/index/index.phtml
<?php echo $this->test ?>
// application/views/scripts/index/chart.phtml
<?php echo $this->test ?>
now test your application by calling the url's:
http://yourDomain.com/index/index
http://yourDomain.com/index/chart
If your setup is correct you will see the proper response in your pages.
Case 1: View disabled for just one action:
Look for the following code in your action.
$this->_helper->viewRenderer->setNoRender(true);
Case 2: View disabled for all actions in a specific controller:
Look for the above line in either the init() or preDispatch() functions of the controller.
Case 3: View disabled for all actions in all controllers:
Check case 1 and 2. Also, look for something like the following in your Bootstrap.php:
$frontController->setParam("noViewRenderer", true);
If you find the code like above, you will have to comment it out to get the view working. I am sure there are more possibilities to disable view. Those are to be checked after this.
Your view is disabled..check these lines of code in your action or init of your controller or even the class your controller may be extending
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
UPDATE
You are doing it in your chartAction and echoing in your index.phtml you must do that in your chart.phtml

CakePHP - Problems with ErrorHandler not extending AppController

I had been wondering why my error page caused certain pages of my site
not to render, but then I realized that it's because AppError extends
ErrorHandler instead of AppController. This caused some variables
that I set in AppController's beforeFilter method not to be sent to
the view. Since I can't access session variables from AppError, I
thought that I might be able to get away with using the classRegistry
to instantiate something that could and simply copying and pasting the
rest of my code from AppController's beforeFilter... but that isn't working, nor does it seem like a very elegant fix. Does anyone have any clues as to what
would be the best way to approach this? Thanks, David.
Your AppError class has a controller instance. You can call the beforeFilter manually:
<?php
class AppError extends ErrorHandler {
function error404() {
$this->controller->beforeFilter();
parent::error404();
}
}
?>
In CakePHP 2, you can do something like this to achieve the same effect. In app/Config/bootstrap.php, add this line:
Configure::write('Exception.renderer', 'AppExceptionRenderer');
Then create a file app/Lib/Error/AppExceptionRenderer.php with this code:
App::uses('ExceptionRenderer', 'Error');
class AppExceptionRenderer extends ExceptionRenderer {
protected function _outputMessage($template) {
$this->controller->beforeFilter();
$this->controller->render($template);
$this->controller->afterFilter();
$this->controller->response->send();
}
}
Described more generally here: http://book.cakephp.org/2.0/en/development/exceptions.html#using-a-custom-renderer-with-exception-renderer-to-handle-application-exceptions
Edit: Updated link to point to correct location of the CakePHP 2.0 Book as of July 05, 2012.

Categories