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.
Related
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
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();
}
I am able to render my specified error pages in my modules, but I can't get error layouts outside of my modules.
It's quite strange, in my main config I have:
'errorHandler'=>array(
'errorAction'=> '//site/error',
),
I tried to see if it is pointed to the right file by changing it to //site/contact, and see if it renders the contact page if error. And it does show.
So the path is correct, then how come it's not showing the error page? It shows the default white page with exception error. The exception page is what im expecting, but how it's formatted is not.
public function actionError()
{
$this->layout = '/layouts/main';
if($error=Yii::app()->errorHandler->error)
{
if(Yii::app()->request->isAjaxRequest)
echo $error['message'];
else
$this->render('error', array('error'=>$error));
}
}
Your user role doesn't have rights to access actionError() in controller.
In your site controller, You can see a function called public function accessRules().
Check error is added for some user role, or it's for admin only.
If error not found, then add it for user roles.
Example:
public function accessRules()
{
return array(
array('allow',
'actions' => array('index', 'view', 'error'),
'users' => array('#')
),
..
..
..
}
You need to create a errorXXX in views/system. The XXX stand for the error code, in this case most likely a 403. For more information see this page on the Yii site.
I just want to know if we can add a config file that extends the main.conf in the module
It does, already. in the CWebModule through $this->setComponents as follows:
<?php
class AccountModule extends CWebModule
{
public function init()
{
// this method is called when the module is being created
// you may place code here to customize the module or the application
$this->setComponents(array(
'errorHandler' => array(
'errorAction' => 'module/default/error'),
'defaultController' => 'default',
'user' => array(
'class' => 'ModuleWebUser',
'allowAutoLogin'=>true,
'loginUrl' => Yii::app()->createUrl('module/default/login'),
)
));
// import the module-level models and components or any other components..
$this->setImport(array(
'module.models.*',
'module.components.*',
));
}
} ?>
The way to do it is to make an array item for your module/etc in the params item of the main config array.
Look at this forum post: http://www.yiiframework.com/forum/index.php/topic/24617-custom-configuration/
if you want your configuration to be in a separate file you can merge it with the main config array in the config file!
something like this should work:
include("custom_config.php"); // define $array_from_custom_conf inside this file
return array_merge(
array(/*main_config_array from main.php*/),
$array_from_custom_conf
);
If you put your custom config array as the 2nd argument it will also overwrite attributes from the main config.
I've never did it but:
A current solution is provided in a wiki article.
Regarding this 'feature request', its not a big surprise that this was already requested on Yii's forums. See here and here.
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'));