How do you include the javascript helper in the ErrorHanlder? - php

I have this custom error handler:
`class AppError extends ErrorHandler {
function error404($params) {
$this->controller->layout = 'public';
$this->controller->set('title','Droptor Page Not Found');
parent::error404($params);
}
}`
And I can't seem to use any layout that has this:
$javascript->link('jquery',true)
So the JS helper isn't loaded. But if I include this in the controller: var $helpers = array('javascript'); it still doesn't work. Nor does App::import('Helper', 'javascript');

Crap, I didn't read your question.
To add a helper to your error controller, just add this line:
$this->controller->helpers = array('Javascript');
There are two ways to do it:
First, you can create an app_controller to include every component and helper that you need on all your controllers.
Second, you can load the specific resources needed to your error controller. Create a file named error.php in your app's root (NOT webroot) with the following code:
<?php
class AppError extends ErrorHandler {
function error404($params) {
$this->controller->helpers = array('Javascript');
parent::error404($params);
}
}
You can also set a custom title with
$this->controller->set('title_for_layout', "We couldn't find what you are loooking for");
Good luck.

First off, you don't need to create your own handler if all you're doing is handling a common error type, such as 404. Custom error handlers are for your application specific errors.
If you want to simply change the layout of your page when you get a 404 error, this has been answered over here.
function beforeRender() {
if($this->name == 'CakeError') {
$this->layout = false;
}
}
And you can cause it using the line:
$this->cakeError('error404');

Related

can we remove default layout of view in yii 1.1

I am new in yii, and trying to import a existing PHP site into this framework, so i want to remove default layout style of yii, just wanna show my view page.
Is there any way to do so?
like, when i load a view
$this->render('myview');
then only myview.php should be render.
I didn't found any help anywhere.
Just put $this->layout = false; in your action or a property public $layout = false; in your controller if you want it disabled controller wide.
Use the renderPartial function.
$this->renderPartial('myview', array('model'=>$model));
You can do other things, like assign the markup to a variable and do things like echo, manipulate and save it.
The renderPartial will not load the page layout.
// for disabled in whole controller actions use following method in controller
Class siteController extend controller {
Public $layout = false;
}
// disable for particular action in controller
Class siteController extend controller {
Public actionIndex (){
$this->layout = false.
}
}

Load a single module of page in views codeigniter framework

I am developing a web app using codeigniter framework. My application has a fixed header and footer.I want to middle section (ie the body) of my application to load when the user goes to various pages which are available to him and make the header and footer constant.
(hackerrank.com ... I was talking about a website similar to this one ... after login into this website ... the header and the sidebar of this remains constant and they load the remaining page ... how can we implement it using CI framework)
Is there any way through which I can achieve this?
I am performing the following actions which is making me to load the complete website(Its like reloading the entire page :/)
As You can see the following code is present in my template.php
<?php
$this->load->view("templates/header.php");
$this->load->view($main_body);
$this->load->view("templates/footer.php");
?>
and I Controller I write the following piece of code usually
public function load_page(){
$data['main_body'] = 'dashboard_pages/dashboard_view';
$this->load->view('template.php',$data);
}
You were pretty close to the solution: Load the header and the footer as they are, variables:
<?php
$this->load->view($header);
$this->load->view($main_body);
$this->load->view($footer);
?>
The tricky thing, is that you're always writing the method function load_page() in every controller you write, and it would be better having a MY_controller, a class previous to your controller class in which you'll write which footer you're referring:
Check for writing a MY_Controller here: http://ellislab.com/codeigniter/user-guide/general/core_classes.html
Then, write your MY_Controller:
class MY_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
}
public function load_page( $page )
{
$data['main_body'] = $page;
// Logic for your header or footer deppending on logging or whatever...
if ( 1 ==1 ) {
$data['header'] = "templates/header.php";
$data['footer'] = "templates/footer.php";
}
$this->load->view('template.php',$data);
}
}
and you'll have to make sure your controllers extends MY_Controller class, and add a load_page method to whom you'll pass the argument:
class Custom_page extends MY_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$this->load_page( 'dashboard_pages/dashboard_view' );
}
}
I think with that you'll have exactly what you look for, that way you only have to write in one place the logic for the header and footer: In MY_Controller, and just use it in any part.

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.

Controller specific layout in ZendFramework 2

I have a module in my zendframework 2 application which contains two controllers.
I want to set a different layout for one of the controller's actions.
Is there a way to set it inside module config file?
P.s: I just tried to set it inside controller's __CONSTRUCT method using the following commands but it just didnt worked!
$event = $this->getEvent();
$event->getViewModel()->setTemplate('layout/MYLAYOUT');
But if i use the above commands inside each action of my controller it just works fine.
See akrabat's examples for a number of nice ways that layouts, views, etcetera can be tweaked easily.
Specifically what you're looking for can be found on his github here.
Here is a cut-paste of the controller's action method that sets/uses the alternate layout:
public function differentLayoutAction()
{
// Use a different layout
$this->layout('layout/different');
return new ViewModel();
}
Edit: It looks like akrabat has an example that says Change the layout for every action within a module, which might give the best pointers for setting the layout in the config; but I just had a look at the code, and the example is currently unfinished, it's not changing the layout.
I can just point you into the right direction, since currently i'm unable to open a sample project. Evan Coury has posted a method for Module specific layouts. See the following links:
Module Specific Layouts in Zend Framework 2
<?php
namespace MyModule;
use Zend\ModuleManager\ModuleManager;
class Module
{
public function init(ModuleManager $moduleManager)
{
$sharedEvents = $moduleManager->getEventManager()->getSharedManager();
$sharedEvents->attach(__NAMESPACE__, 'dispatch', function($e) {
// This event will only be fired when an ActionController under the MyModule namespace is dispatched.
$controller = $e->getTarget();
$controller->layout('layout/alternativelayout');
}, 100);
}
}
Now how would this help you?: Well, $controller should have both the called controller and action stored. I'm sure you can check the $controller for the called action and then assign the layout accordingly.
I'm sorry i can currently only hint you into the direction, but i'm sure this can get you started.
#Sam's answer pretty much answers the question. As stated it just needs a check on which controller is called, which can be done like this:
<?php
namespace MyModule;
use Zend\ModuleManager\ModuleManager;
class Module
{
public function init(ModuleManager $moduleManager){
$sharedEvents = $moduleManager->getEventManager()->getSharedManager();
$sharedEvents->attach(__NAMESPACE__, 'dispatch', function($e) {
$controller = $e->getTarget();
if ($controller instanceof Controller\AltLayoutController) {
$controller->layout('layout/alternativelayout');
}
}, 100);
}
I

CakePHP error 404

How can I have a custom layout for a 404 page in Cake? I know you can create your own view but I also want a custom layout for it as I don't want it inheriting my site design and want it to have a unique look and feel.
I've created my own views and then added my own app_error in /app/ with the following code:
<?php
class AppError extends ErrorHandler
{
function error()
{
$this->layout = 'error';
}
}
?>
But it doesn't load the error layout? Any ideas why?
Thanks.
Create your own AppError class (in app/app_error.php and override the _outputMessage method, something like:
class AppError extends ErrorHandler {
function _outputMessage($template) {
$this->controller->render($template, 'NAME OF THE LAYOUT');
$this->controller->afterFilter();
echo $this->controller->output;
}
}
I believe you just create a file at /views/errors/error404.thtml. It may end in .ctp actually, but give both a shot.
Create/edit the app/views/errors/error404.ctp page. See CakePHP Error Handling.

Categories