Redirecting to 404 route in PhalconPHP results in Blank Page - phalcon

I have setup a router, and in it, defined a route for 404s:
<?php
use Phalcon\Mvc\Router;
$router = new Router(FALSE);
$router->removeExtraSlashes(true);
$route = $router->add('/', ['controller' => 'index', 'action' => 'index']);
$route->setName("index");
// other routes defined here...
$router->notFound([
"controller" => "index",
"action" => "route404"
]);
?>
My IndexController:
<?php
class IndexController extends ControllerBase
{
public function indexAction()
{
// code removd for berevity
}
public function route404Action() {
// no code here, I just need to show the view.
}
}
?>
And I have a view # /app/views/index/route404.phtml that just has a bit of HTML in it, I even tried making it a .volt file, no luck.
When I go to a page that doesn't match any routes, it works fine. But, if I try to redirect to it, I just get a blank page. For example, in one of my controllers I have this:
if (!$category) {
// show 404
//Tried this next line to test, and it indeed does what you'd expect, I see "Not Found".
// echo "Not Found"; exit;
$response = new \Phalcon\Http\Response();
$response->redirect([
"for" => "index",
"controller" => "index",
"action" => "route404"]
);
return; // i return here so it won't run the code after this if statement.
}
Any ideas? The page is completely blank (nothing in source) and there are no errors in my apache logs.

Try returning the response object, not just a blank return. Example:
return $this->response->redirect(...);
However I would recommend to use Forward from dispatcher to show 404 pages. This way user will stay on same url and browser will receive the correct status code (404). Also it's SEO friendly this way :)
Example:
if ($somethingFailed) {
return $this->dispatcher->forward(['controller' => 'index', 'action' => 'error404']);
}
// Controller method
function error404Action()
{
$this->response->setStatusCode(404, "Not Found");
$this->view->pick(['_layouts/error-404']);
$this->response->send();
}

Related

How to change yii2 default module

Hello I am not an expert in Yii2 and would appreciate any help, We want to change our default module,
Our logic:
site implements a use of wildcard domain, https://example.com,
we implement a bootstrap component to Identify a use of a "subdomain" in the
url I.E. https://sub.example.com,
$config = [
'id' => 'basic',
'name' => 'exapmle',
'basePath' => dirname(__DIR__),
'bootstrap' => [
'log',
'devlogin',
'app\components\SubBootstrap', #this is the bootstrap component we use
'app\components\ThemeBootstrap',
],...
now we would have liked to use the same logic to change the default module to a new "submodule" but we can't use the bootstrap because it happens after the default module has been applied.
obviously we can have an explicit url call for the module I.E.
'modules' => [
'sub'=>[
'class' => 'app\modules\sub\Module',
],...
but that means that the url would look like https://somesub.example.com/sub/ which is undesirable.
thank you very much
In your case, what you can do is override the UrlManager component and manually adjust the path to reflect the module that you want to envoke behind the scenes.
So your code would look something like this:
<?php
namespace app\components;
use Yii;
class UrlManager extends \yii\web\UrlManager
{
public function parseRequest($request)
{
if (!empty(Yii::$app->sub)) {
$pathInfo = $request->pathInfo;
$moduleIds = array_keys(Yii::$app->modules);
$inModule = false;
foreach ($moduleIds as $moduleId) {
if (preg_match("/^{$moduleId}/", $pathInfo)) {
$inModule = true;
break;
}
}
if (!$inModule) {
$pathInfo = 'sub/' . $pathInfo;
$request->setPathInfo($pathInfo);
}
}
return parent::parseRequest($request);
}
}
and then in config/web.php:
'urlManager' => [
'class' => 'app\components\UrlManager',
...
],
You don't need to change the module configuration. You need to change web-server path to this module and architech the UrlManager rules;
https://www.yiiframework.com/doc/guide/2.0/en/runtime-routing
Bootstraping yii module it's only some way to load it before another components.
https://www.yiiframework.com/doc/guide/2.0/en/runtime-bootstrapping

a route doesnt work (yii)

Hy stackoverflow !
I'm trying to make a form into an external page with Yii 1.1.14 (this is an old site).
I've made a directory into my views call signinPartners and into this one, a php file call signin.
I have also created a controller :
class SigninPartnerController extends Controller
{
public function actionSignin(){
$this->render('/signin');
}
}
He renders the route /signin defined in my config by :
return array(
'' => 'index',
'signin' => 'signinPartners/signin',
);
but when I try the URL http://mylocalserver/signin the site send back Error 404 Unable to resolve the request « signin-partners/signin »..
This is really disturbing because there are other URL's on my website and they work the same way without throwing an error 404. I don't know what I missed... Can somebody help ?
my urlManager :
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName' => false, // do not display index.php in the url
'urlSuffix' => '/',
'rules' => $routesRules, //this variable contains the array defined above
),
I have also check my runtime :
2018/01/17 11:12:16 [error] [exception.CHttpException.404] exception 'CHttpException' with message 'Impossible de résoudre la requête « signinPartners/signin ».' in D:\Windows\Windows\CommonFiles\wamp64\www\MoovTime-Conso\library\Yii\web\CWebApplication.php:286
Stack trace:
#0 D:\Windows\Windows\CommonFiles\wamp64\www\MoovTime-Conso\library\Yii\web\CWebApplication.php(141): CWebApplication->runController('signinPartners...')
#1 D:\Windows\Windows\CommonFiles\wamp64\www\MoovTime-Conso\library\Yii\base\CApplication.php(183): CWebApplication->processRequest()
#2 D:\Windows\Windows\CommonFiles\wamp64\www\MoovTime-Conso\public\frontend\index.php(36): CApplication->run()
#3 {main}
REQUEST_URI=/signin
---
(the exception message is in french and means Unable to resolve the request « signin-partners/signin »)
Ok, big update, i've tried to play with routes.php and I realized that the name of my controller doesn't match with signinPartners. So, I update the routes rules with :
return array(
'' => 'index',
'signin' => 'signinPartner/signin',
);
And now, we have a new error : Controller can't find the view « /signin »..
There is the post Controller can't find the view in Yii which can anwser this question !
ANSWERED
try adding a - in signinPartners.
'signin-partners/signin'
The problem in my case was the action, the directory name in my view that contains the page and the route rule.
first, I update the route rule from :
return array(
'' => 'index',
'signin' => 'signinPartners/signin',
);
to :
return array(
'' => 'index',
'signin' => 'signinPartner/signin', // controlerName/actionName
);
The controller name was wrong.
In a second time, to match with the name of the controller, I simply renamed the directory in views calls signinPartners to signinPartner (only for readability).
Finally I update the action actionSignin that was :
public function actionSignin(){
$this->render('/signin');
}
For :
public function actionSignin(){
$this->render('/signinPartner/signin');
}
Where I change the path with the new directory name.

Yii2 Captcha dosent render and show raw data image

Captcha in backend is configured and has worked.
But with same configuration does not work on front-end and show raw image data, like in the picture.
Access roles are correct and captcha action does not have any additional config.
PHP GD is already active in my host
Yii2 Captcha show RAW data
Two things you might want to check.
First, did you override the actions() method in your controller class? You'll need to add the following:
class YourController extends Controller
{
public function actions()
{
return array(
'captcha' => array(
'class' => 'CCaptchaAction',
'backColor' => 0xFFFFFF,
),
);
}
}
If you did that and it still doesn't work, check your controller access. When you are overwritten accessRules(), you need to make the captcha action available for everyone, like this:
class YourController extends Controller
{
public function accessRules() {
return array('allow', 'actions' => array('captcha'), 'users' => array('*'));
}
}
Hope this helps !.
ob_clean();
please try before you show captcha or any other suitable place.

Yii url rules for main page

'urlManager'=>array(
'class'=>'application.components.UrlManager',
'urlSuffix'=>'/',
'baseUrl'=>'',
'showScriptName'=>false,
'urlFormat'=>'path',
'rules'=>array(
'<language:\w{2}>' => 'page/index',
'' => 'page/index',
'<language:\w{2}>/page/<alias:.*>' => 'pages/read',
)
link "/en/page/index" works fine
links "/" and "/en" returns the error "Unable to resolve the request" page / index ".
what is wrong with the rules
'<language:\w{2}>' => 'page/index'
'' => 'page/index',
?
UPD:
pagesController has an action:
public function actionRead($alias){
//some php code...
if($model==null)
{
throw new CHttpException(404,'page not found...');
}else
{
$this->render('read',array('model'=>(object)$model));
}
}
Your rules that don't work are redirecting to page/index, meaning that they're going to try and access PageController.php, and within that controller, they're going to try an access actionIndex. It doesn't sound like you have either a controller PageController.php, much less an actionIndex within that controller.
You need to fix the targets of those rules to include valid controller/action combinations.

How to remove auth from the pages controller in CakePHP?

I'm using CakePHP's Auth component and it's in my app_controller.php.
Now I want to allow specific views from the pages controller. How do I do that?
Copy the pages_controller.php file in cake/libs/controllers to your app/controllers/ dir. Then you can modify it to do anything you want. With the auth component, the typical way to allow specific access is like this:
class PagesController extends AppController {
...
function beforeFilter() {
$this->Auth->allow( 'action1', 'allowedAction2' );
}
...
I recommend highly copying the file to your controllers dir, rather than editing it in place, because it will make upgrading cake much easier, and less likely that you accidentally overwrite some stuff.
You could add the following to your app_controller.
function beforeFilter() {
if ($this->params['controller'] == 'pages') {
$this->Auth->allow('*'); // or ('page1', 'page2', ..., 'pageN')
}
}
Then you don't have to make a copy the pages controller.
I haven't tried the other ways but this is also the right way to allow access to all those static pages as display is that common action.
In app_controller:
//for all actions
$this->Auth->allow(array('controller' => 'pages', 'action' => 'display'));
//for particular actions
$this->Auth->allow(array('controller' => 'pages', 'action' => 'display', 'home'));
$this->Auth->allow(array('controller' => 'pages', 'action' => 'display', 'aboutus'));