404 router in Lithium PHP - php

I know how to make response routes in the actual /config/routes.php file, but I can't find where to change the default 'fetal dispatcher' error. I'd like to be able to have it route to a nice 404 page I've made when there's a missing page/action controller. Is that possible?

Yes, you can take advantage of lithium\core\ErrorHandler for this. See the code in the default config/bootstrap/errors.php:
ErrorHandler::apply('lithium\action\Dispatcher::run', array(), function($info, $params) {
$response = new Response(array(
'request' => $params['request'],
'status' => $info['exception']->getCode()
));
Media::render($response, compact('info', 'params'), array(
'library' => true,
'controller' => '_errors',
'template' => 'development',
'layout' => 'error',
'request' => $params['request']
));
return $response;
});
This is saying, if any exception occurs during Dispatcher::run(), display the development.html.php template from the views/_errors folder with the layouts/error.html.php layout.
So you can change that -- maybe you check the Environment to see if this is a dev or production environment and display a different template for production.
Maybe if $info['exception']->getCode() === 404, you can switch to a template specifically for 404 errors.

Related

Drupal 7 Messages only show after class registry clear

Need to show some messages from hook_block_view.
so in the function setting a message like:
drupal_set_message('Block should have loaded');
Does not work.
If I clear class registry, it works once, then every other time it does not show.
It looks like drupal is redirecting or something before the page is rendered. Using drupal_exit(); by the end of this function does show everything is correct so far, but does not make it to the final output.
Edit:
It works if I throw in drupal_flush_all_caches But then goes super slow obviously. Seems to be some sort of caching problem.
Turn off full caching for the block under hook_block_info() by setting 'cache' to: DRUPAL_NO_CACHE
function hook_block_info() {
$blocks = array();
$blocks['abc'] = array(
'info' => t('test block'),
'visibility' => 0,
'status' => TRUE,
'region' => 'none',
'weight' => 99,
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
Then try to write
drupal_set_message('Block should have loaded');
within hook_block_view and check now...
it may be help for you.

Yii create first test page

I am new to yii i have setup of sample yii demo application (default)n now i want to create new page but i am getting 404 error following are the steps i have followed.
1) In site Controller created new action
public function actionTest()
{
$this->render('test');
}
2) created new page in view folder test.php
echo "test";
3) In main.php (config file) added following code
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'caseSensitive' => false,
'rules' => array(
'test' => 'site/test',
),
),
4) when i run the application with URL
http://localhost/YiiTest/test
then got the error
The requested URL /YiiTest/test was not found on this server.
make sure you have rewrite your .htaccess file link

How does the default_target_path work in a Silex Firewall?

I have setup a firewall in Silex as follows:
$this -> register(new SecurityServiceProvider(), array(
'security.firewalls' => array(
'login' => array(
'pattern' => '^/admin/login$'
),
'admin' => array(
'pattern' => '^/admin.*$',
'form' => array(
'login_path' => '/admin/login',
'check_path' => '/admin/security/validate',
'default_target_path' => "/admin",
'always_use_default_target_path' => true
),
'logout' => array(
'logout_path' => '/admin/security/logout'
),
'users' => $app -> share(function () use ($app) {
return new \Turtle\Providers\UserProvider($app);
})
)
),
'security.access_rules' => array(
array('^/admin.*$', 'ROLE_ADMIN')
)
));
This works in that when I hit a page in the 'admin' area I get redirected to my login page. However I have started to do some authorization in my custom AuththenticationSuccess handler. I want to use the built in method determineTargeUrl to redirect on success but it keeps redirecting to '/'.
After some debugging I have found that the options in the object that the method uses has the following:
array (size=5)
'always_use_default_target_path' => boolean false
'default_target_path' => string '/' (length=1)
'login_path' => string '/login' (length=6)
'target_path_parameter' => string '_target_path' (length=12)
'use_referer' => boolean false
Clearly this is not what I have set in my firewall. It is my understanding that this should match what it is in the firewall that I have used when accessing the system. The URL I used was 'http://localhost/admin'.
So how do I make it so that the options I have set in my firewall appear in the object so that I can use the determineTargetUrl?
Thanks lots, Russell
It looks like your problem is that your login route is inside your secured area. You've defined your access rule as ^/admin.*$. This means that any route starting with /admin requires ROLE_ADMIN including your login route. To fix this you need to remove security from your login route.
Add a new access rule above your admin rule.
'security.access_rules' => array(
array('^/admin/login$', 'IS_AUTHENTICATED_ANONYMOUSLY'),
array('^/admin.*$', 'ROLE_ADMIN')
)
Edit: After reading your question again, I may have misunderstood you. it sounds like you can successfully log in but are redirected to the wrong place after a successful login. Is that correct? If so I will remove this answer.
This was my mistake. I am using custom AuthenticationSuccess and AuthenticationFailure handlers and i neglected to pass any options into them when I was declaring them:
$app['security.authentication.success_handler.admin'] = $app -> share(function() use ($app) {
return new AuthenticationSuccessHandler($app['security.http_utils'], array(), $app);
});
$app['security.authentication.failure_handler.admin'] = $app -> share(function() use ($app) {
return new AuthenticationFailureHandler($app['kernel'], $app['security.http_utils'], array(), $app);
});
So the options array that is used in determineTargetUrl on authentication success was empty and thus had default values.
By adding an array of options to the AuthenticationSuccessHandler it works. This is OK as each custom handler is linked to a different firewall.

Zend Framework 2 - Why i can't override MvcEvent's MatchRoute?

i would like to match all requests where user is unlogged to controller Admin\Controller\Sign and action in. I wrote this code in onBootstrap() method in Module.php file :
if (!$authService->hasIdentity()) {
$routeMatch = new RouteMatch(
array(
'controller' => 'Admin\Controller\Sign',
'action' => 'in'
)
);
$event->setRouteMatch($routeMatch);
}
I don't get any errors, but code doesn't work, why?
The problem here is that the application route event (MvcEvent::EVENT_ROUTE) is triggered after the (MvcEvent::EVENT_BOOTSTRAP).
Which means even if you're setting the route match at the bootstrap level, the application is going to override it with the route match of the request after the MvcEvent::EVENT_ROUTE.
If you want to avoid this overriding you need to add a listener for the route event with a very low priority to make sure it will not be overridden:
$e->getApplication()->getEventManager()->attach(MvcEvent::EVENT_ROUTE, array($this, 'onRouteEvent'), -1000000);
Note : the onRouteEvent would be the method of your Module class that handles the route event (similar to your code).
If you want to short-circuit your application running at the bootstrap level, what you can do is to send the headers with redirection code to the client:
//get the url of the login page (assuming it has route name 'login')
$url = $e->getRouter()->assemble(array(), array('name' => 'login'));
$response=$e->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
$response->sendHeaders();
add a route entry sign_in as below in the routes section of the module.config.php under admin module
'sign_in' => array(
'type' => 'Segment',
'options' => array(
'route' => '/admin/sign/in',
'defaults' => array(
'controller' => 'sign',
'action' => 'in',
),
),
),
and call the route in the controller like this
$this->redirect()->toRoute('sign_in');

CakePHP fails loading $ajax->form with Error 503 Service Unavailable

When I try to make an $ajax->form() call within my view, the server responds with: Error 503 Service Unavailable.
I have loaded:
App::Import('Ajax');
$ajax = new AjaxHelper();
(Within my view)
And then:
$ajax->form(array('type' => 'post',
array('type' => 'post',
'options' => array(
'model'=>'User',
'update'=>'dateTarget',
'url' => array(
'controller' => 'comments',
'action' => 'edit'
)
)
));
The only error I can seem to find is:
Undefined property: AjaxHelper::$Form
From within app/tmp/logs/debug.log
It should be noted that I tried echo'ing: get_class_methods($ajax) and it showed that form IS available.
Can someone advise me on how to proceed from here?
Thanks!
The AjaxHelper depends on the FormHelper (plus the HTMLHelper and JavascriptHelper). If you manually create an instance of the AjaxHelper you also have to create those dependencies (depending on the functionality you intend to use). It's done in the following way:
App::Import('Ajax');
$ajax = new AjaxHelper();
$ajax->Form = new FormHelper();
However, usually the helpers you want to use are added to the $helpers array of your controller(s):
public $helpers = array('Ajax');
See also http://book.cakephp.org/view/1096/Using-Helpers

Categories