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'));
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
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.
Is there a way to configure Yii such that it will no longer load any Javascript out of the Assets folder?
Make your own AssetManager or extend current
protected/components/DummyAssetManager.php:
class DummyAssetManager extends CApplicationComponent {
public function publish(){}
}
add into components array in
protected/config/main.php:
'assetManager'=>array(
'class'=>'DummyAssetManager',
),
You should consult the manual for a detailed description of
the assetManager options
I think you can try following option in your config/main.php
'components' => array(
'assetManager' => array(
'linkAssets' => true,
),
),
This will make asset files symbolic links to your original js/css sources. See linkAssets for more details on it.
If your PHP<5.3, or the OS it's running on doesn't support symbolic links, you won't be able to use 'linkAssets' option, in this case you can try:
'components' => array(
'assetManager' => array(
'forceCopy' => true,
),
),
This should update asset folder on every request. These two options are usually used during development process (btw, you can't use both) and should be removed from production.
PS: if you're sure that you haven't explicitly enabled ckeditor somewhere in your code and you're confident about your assetmanager calls throughout the code, check your layout and page for widgets that require this CKeditor, as Yii can't preload 'stuff' just randomly, it can be triggered by some preloaded component/extension or yii widget.
I need to use a file manager in pop up which comes on a button click. I am using Yii extension elfinder. I am finding it hard to understand the way of using it. I downloaded the code from bitbucket, put it inside my application in the folder extension. I try to test it using new controller, named it elfcontroller and put the following code (got from the website)
class ElfinderController extends CController
{
public function actions()
{
return array(
'connector' => array(
'class' => 'ext.elFinder.ElFinderConnectorAction',
'settings' => array(
'root' => Yii::getPathOfAlias('webroot') . '/uploads/',
'URL' => Yii::app()->baseUrl . '/uploads/',
'rootAlias' => 'Home',
'mimeDetect' => 'none'
)
),
);
}
}
and i created one more function for rendering the index page(i want the file manager to be in this page)
in the view i wrote the following code
$model = new xxxmodel();
$this->widget('ext.elFinder.ElFinderWidget', array(
'model' => $model,
'attribute' => 'serverFile',
'connectorRoute' => 'admin/elfinder/connector',
)
);
and i included a div for containing it
But i am getting the following error
Alias "ext.elFinder.ElFinderWidget" is invalid. Make sure it points to an existing PHP file and the file is readable.
i tried to include alias in config/main.php
I know i am messing some where with the folder structure
here is the path i am using the extension
C:\xampp\htdocs\project\protected\extensions\ext.elfinder
I returned empty after google searches, can any one please explain me how to use this extension to place the code exactly where it is needed to be?
In general the extensions folder already has the ext alias, so you don't need to set an alias for it.
Then the extension itself should be placed in the extensions folder, somewhat like : project/extensions/extension-name/ . In your case it should be: project\extensions\elFinder , and keep the rest of your code same, i.e continue referring to extension like:
ext.elFinder.ElFinderWidget
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.