I'm try to load the Yii Bootstrap extension in the admin module only but it's not working. I'm assuming I need to preload it or initiate it somehow... Thanks!
class AdminModule extends CWebModule
{
public function init()
{
// import the module-level models and components
$this->setImport(array(
'admin.models.*',
'admin.components.*',
'ext.bootstrap.components.Bootstrap',
));
}
public function beforeControllerAction($controller, $action)
{
if(parent::beforeControllerAction($controller, $action))
{
$this->layout = 'admin';
return true;
}
else
return false;
}
}
There are 4 different ways to do this:
Add the configuration to the app's configuration (protected/config/main.php):
'modules'=>array(
'admin'=>array(
'preload'=>array('bootstrap'),
'components'=>array(
'bootstrap'=>array(
'class'=>'ext.bootstrap.components.Bootstrap'
)
),
// ... other modules ...
)
Preload it in the init :
public function init()
{
// import the module-level models and components
$this->setImport(array(
'admin.models.*',
'admin.components.*',
// 'ext.bootstrap.components.Bootstrap', // this will go to app config for components
));
Yii::app()->getComponent('bootstrap');// this does the loading
}
Preload in init another way:
public function init()
{
// import the module-level models and components
$this->setImport(array(
'admin.models.*',
'admin.components.*',
));
$this->configure(array(
'components'=>array(
'bootstrap'=>array(
'class'=>'ext.bootstrap.components.Bootstrap'
)
)
));
$this->getComponent('bootstrap');
}
Preload in init yet another way:
public function init()
{
// import the module-level models and components
$this->setImport(array(
'admin.models.*',
'admin.components.*',
));
$this->configure(array(
'preload'=>array('bootstrap'),
'components'=>array(
'bootstrap'=>array(
'class'=>'ext.bootstrap.components.Bootstrap'
)
)
));
$this->preloadComponents();
}
When you configure the following in the main config (protected/config/main.php), example as:
'admin' => array(
'preload'=>array('bootstrap'),
'components'=>array(
'bootstrap' => array(
'class' => 'bootstrap.components.Bootstrap',
'responsiveCss' => true,
),
),
)
Then, you can using component as
$bootstrap = $this->getModule()->bootstrap; // with $this is a current controller.
To define this module component to main component, you must to set Component in init() of Module class:
// Set main component `bootstrap` instead of CController->getModule()->bootstrap
app()->setComponent('bootstrap', $this->bootstrap);
Really working code :
in your module init function just paste the following code :
Yii::setPathOfAlias('bootstrap', Yii::getPathOfAlias('ext.bootstrap'));
Yii::app()->setComponent('bootstrap', array('class'=>'bootstrap.components.Bootstrap'));
protected/config/main.php
'modules'=>array(
'admin'=>array(
'preload'=>array('bootstrap'),
'components'=>array(
'bootstrap'=>array(
'class'=>'ext.bootstrap.components.Bootstrap'
)
),
)
Preload it in the init :
public function init()
{
// import the module-level models and components
$this->setImport(array(
'admin.models.*',
'admin.components.*',
// 'ext.bootstrap.components.Bootstrap', // this will go to app config for components
));
Yii::app()->getComponent('bootstrap');// this does the loading
}
If it doesn't work then paste your url
into that file, which form you are using bootstrap
<link rel="stylesheet" type="text/css" href="/pojects/fcorginal/assets/b2e88ad5/bootstrap/css/bootstrap.min.css" />
Related
To override error handler from a module based this samdark guid:
class Module extends \yii\base\Module
{
public function init()
{
parent::init();
\Yii::configure($this, [
'components' => [
'errorHandler' => [
'class' => ErrorHandler::className(),
]
],
]);
/** #var ErrorHandler $handler */
$handler = $this->get('errorHandler');
\Yii::$app->set('errorHandler', $handler);
$handler->register();
}
}
This method only worked for valid module controllers, but can not handle invalid module controllers. Even affects all over the application so that module error handler will be default error handler all over the application. i was confused how can solve it!
Example: I was willing www.site.com/module/x handled with module error handler(www.site.com/module/default/error),where x is not a valid module controller. If that was not the case, it was handled with app error handler(www.site.com/site/error).
Finally i used this code: (admin is my module name). Know everything is fine.
class AdminModule extends \yii\base\Module
{
public function init()
{
parent::init();
// Is better used regex method, temporally i used this method
$r = Yii::$app->urlManager->parseRequest(Yii::$app->request)[0];
$r_array = explode('/',$r);
if($r_array[0] === 'admin'){
Yii::configure($this, [
'components' => [
'errorHandler' => [
'class' => ErrorHandler::className(),
'errorAction' => '/admin/default/error',
]
],
]);
$handler = $this->get('errorHandler');
Yii::$app->set('errorHandler', $handler);
$handler->register();
}
}
}
If you know a better way please share with me. Thanks
I am making a module and I need to make an ajax request, with JSON response if possible, how can i do this ?
I don't understand really well the structure of Prestashop 1.7 on this.
Thanks !
This is pretty simple, you just have to make the controller with Prestashop's standards then link it to your frontend Javascript.
Name a php file like this : ./modules/modulename/controllers/front/ajax.php
Then put inside :
<?php
// Edit name and class according to your files, keep camelcase for class name.
require_once _PS_MODULE_DIR_.'modulename/modulename.php';
class ModuleNameAjaxModuleFrontController extends ModuleFrontController
{
public function initContent()
{
$module = new ModuleName;
// You may should do some security work here, like checking an hash from your module
if (Tools::isSubmit('action')) {
// Usefull vars derivated from getContext
$context = Context::getContext();
$cart = $context->cart;
$cookie = $context->cookie;
$customer = $context->customer;
$id_lang = $cookie->id_lang;
// Default response with translation from the module
$response = array('status' => false, "message" => $module->l('Nothing here.'));
switch (Tools::getValue('action')) {
case 'action_name':
// Edit default response and do some work here
$response = array('status' => true, "message" => $module->l('It works !'));
break;
default:
break;
}
}
// Classic json response
$json = Tools::jsonEncode($response);
echo $json;
die;
// For displaying like any other use this method to assign and display your template placed in modules/modulename/views/template/front/...
// Just put some vars in your template
// $this->context->smarty->assign(array('var1'=>'value1'));
// $this->setTemplate('template.tpl');
// For sending a template in ajax use this method
// $this->context->smarty->fetch('template.tpl');
}
}
?>
In your Module Hooks, you need to bring access to the route in JS, so we basicaly make a variable :
// In your module PHP
public function hookFooter($params)
{
// Create a link with the good path
$link = new Link;
$parameters = array("action" => "action_name");
$ajax_link = $link->getModuleLink('modulename','controller', $parameters);
Media::addJsDef(array(
"ajax_link" => $ajax_link
));
}
On the frontend side, you just call it like this in a JS file (with jQuery here) :
// ajax_link has been set in hookfooter, this is the best way to do it
$(document).ready(function(){
$.getJSON(ajax_link, {parameter1 : "value"}, function(data) {
if(typeof data.status !== "undefined") {
// Use your new datas here
console.log(data);
}
});
});
And voila, you have your ajax ready to use controller
I have following implicit route defined (Laravel 5.2)
// Handle locale
Route::group([
'prefix' => '{country}/{language}',
], function () {
Route::controller('user', 'UserController');
});
And here is my controller
class UserController extends BaseLocaleController
{
public function getIndex()
{
return view('user/index');
}
public function getProfile($slug)
{
echo $slug;die;
return view('user/view');
}
}
My URI Structure is
http://{host}/in/en/user/profile/manju
The problem here, my slug value is in instead of manju. Is there any URI pattern I need to apply?
How can make this work in Laravel 5.2. As you could see, I have country and language prefix in Route::group.
Just pass the $country, $language to the method
So it should be
public function getProfile($country, $language, $slug)
{
echo $slug;die;
return view('user/view');
}
Here is my code
/**
* Setting up the view component
*/
$di->setShared('view', function () use ($config) {
$view = new View();
$view->setViewsDir($config->application->viewsDir);
$view->registerEngines(array(
'.volt' => function ($view, $di) use ($config) {
$volt = new VoltEngine($view, $di);
$volt->setOptions(array(
'compiledPath' => $config->application->cacheDir,
'compiledSeparator' => '_'
));
$volt->getCompiler()->addFunction(
'paymentStatus',
function($key)
{
return #"Info::paymentStatus({$key})";
}
);
return $volt;
},
'.phtml' => 'Phalcon\Mvc\View\Engine\Php'
));
return $view;
});
And the error message (expected to be honest to say)
Strict Standards: Non-static method Info::paymentStatus() should not be called statically, assuming $this from incompatible context in /home/zxcvbnm/public_html/app/cache/_home_zxcvbnm_public_html_app_views_invoice_admininvoice.volt.php on line 49
How can I call method dynamically?
If you want to call your method is statically, you must change the method implementation to static:
public static function paymentStatus($key){
...code
}
I created a modules called calculator and am trying to create the freindly url for it.
here is my code
'<action:(calc1|calc2|calc3)>' => '/calculator/<action>',
the view files are in
modules/calculator/view/default/pages/calc2.php
when i type in my address bar
localhost/dev/cal2
i get this error
Error 404
Unable to resolve the request "calculator/calc2".
in my controller i have this
class DefaultController extends Controller
{
public function actions() {
return array(
'page' => array('class' => 'CViewAction'),
);
}
public function actionIndex()
{
$this->render('index');
}
}
the default page shows when i go to http://localhost/dev/calculator but the inner pages in the modules doesn't.
here is my regex
'<view:(about|terms|faq|privacy)>' => 'site/page',
'<action:(contact|login|logout)>' => 'site/<action>',
'<fileName:(calc1|calc2|calc3)>' => '/calculator/default/page/view/<fileName>',
'<action:(registration|create)>' => 'user/<action>',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
any idea what i'm doing wrong?
In order to use the page action your urls should resolve to /<module>/<controller>/page/view/<fileName> where fileName is calc1, calc2 etc. Therefore in your case your rules should be:
'<fileName:(calc1|calc2|calc3)>' => '/calculator/default/page/view/<fileName>',
Reference: http://www.yiiframework.com/wiki/22/how-to-display-static-pages-in-yii/
First you need to define your action in your calculator controller:
class CalculatorController extends Controller
{
public function actionCalc1()
{
$this->render('calc1');
}
public function actionCalc2()
{
$this->render('calc2');
}
public function actionCalc3()
{
$this->render('calc3');
}
}
Of course you've to change your view paths and then you should see something when you type into your browser <your domain>/calculator/calc2.