I would like to create a global variable, which will be set and get in .tpl file. The global variable should be saved for the user (logged in or not)
Created an override in Override -> classes -> controller -> FrontController.php
And add code :
<?php
use PrestaShop\PrestaShop\Adapter\Cart\CartPresenter;
use PrestaShop\PrestaShop\Adapter\ObjectPresenter;
use PrestaShop\PrestaShop\Adapter\Configuration as ConfigurationAdapter;
use PrestaShop\PrestaShop\Adapter\Image\ImageRetriever;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Debug\Debug;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;
class FrontController extends FrontControllerCore
{
public function initContent()
{
$this->context->smarty->assign(array(
'VARIABLE_NAME' => VARIABLE_VALUE,
));
return parent::initContent();
}
}
?>
Related
I'm trying to redirect to an external URL from a helper class.
Here's my controller:
<?php
namespace App\Http\Controllers;
use App\Lead;
use Illuminate\Http\Request;
use App;
use Helper;
class MyController extends Controller
{
public function entities_get() {
Helper::my_function(); // <---- Call my Helper class method to redirect.
return view( 'template' );
}
}
Here's my helper class used in the controller:
<?php
namespace App\Helpers;
class Helper
{
public static function my_function()
{
return redirect()->away( 'https://www.google.com' ); // <---- Not redirecting.
}
}
Why is the redirect() not working inside my_function()? Do I need to include some Laravel classes using the PHP "use" statement?
You can add send method and it will work.
<?php
namespace App\Helpers;
class Helper
{
public static function my_function()
{
return redirect()->away('https://www.google.com')->send();
}
}
here is my code:
<?php
use Illuminate\Support\Facades\Session;
namespace App\CustomLibrary
{
class myFunctions {
public function is_login() {
if(Session::get('id') != null){
return TRUE;
}
}
}
}
?>
I'm new in laravel 5, i just added a new custom function. And inside that function i wanna check a session ('id)? But i've got an error like this
FatalErrorException in myFunctions.php line 8:
Class 'App\CustomLibrary\Session' not found
I need to know how to use session properly.
Add this clause to the top of the class, right after namespace part:
use Session;
Or use full namespace:
if (\Session::get('id') != null)
Or use the session() helper:
if (session('id') != null)
Your use needs to be after the namespace declaration:
//use Illuminate\Support\Facades\Session; //Not here
namespace App\CustomLibrary
use Illuminate\Support\Facades\Session; //Here
Anything that is used before the namespace is used within the global namespace which changes once a namespace is declared.
use Session; in Model and Controller
// Via a request instance...
$request->session()->put('key', 'value');
// Via the global helper...
session(['key' => 'value']);
for more details https://laravel.com/docs/5.1/session
I am using cakephp v3 and want to ask how to use custom helper in controller
I have tried below code.
namespace App\Controller;
use Cake\Core\Configure;
use Cake\Network\Exception\NotFoundException;
use Cake\View\Exception\MissingTemplateException;
use App\Controller\AppController;
use Cake\Event\Event;
use Cake\Network\Request;
use Cake\ORM\TableRegistry;
use Cake\Auth\DefaultPasswordHasher;
use Cake\View\View;
use Cake\View\Helper\HtmlHelper;
use Cake\View\Helper\UrlHelper;
use Cake\View\Helper;
use App\View\Helper\SecurityHelper;
/**
* Static content controller
*
* This controller will render views from Template/Pages/
*
* #link http://book.cakephp.org/3.0/en/controllers/pages-controller.html
*/
class PagesController extends AppController {
function beforeFilter(Event $event) {
parent::beforeFilter($event);
$this->viewBuilder()->helpers(['Security']);
}
public function receiveSaving() {
pr(($this->request->params['id'])); //die;
pr($this->Security->decrypt('7JCdO3vIqAU_EQUALS_')); die;
$this->viewBuilder()->layout('front');
$this->set('title', '');
$this->set('meta_title', '');
$this->set('meta_description', '');
$this->set('meta_keywords', '');
//$this->render('savings_bond_result');
}
and getting below error.
Error: Call to a member function decrypt() on boolean File
D:\xampp\htdocs\myproject\src\Controller\PagesController.php Line: 204
I tried with some other methods like
$Security = new SecurityHelper(new \Cake\View\View());
pr($Security->decrypt('7JCdO3vIqAU_EQUALS_')); die;
Error: Class 'App\Controller\SecurityHelper' not found File
D:\xampp\htdocs\myproject\src\Controller\PagesController.php Line: 204
$customdtfhelper = new \App\View\Helper\CustomDtfHelper(new \Cake\View\View());
$customdtfhelper->method_name();
I am new to Laravel and checking out some sample code.
In a controller I see this:
<?php
use Illuminate\Support\Facades\Input;
class RegistrationController extends \BaseController {
public function __construct()
{
$this->beforeFilter('guest');
}
Why do I have to use the "use Illuminate\Support\Facades\Input;" ?
Cant I just use eg Input::get(); like I do in my route file?
<?php
use Illuminate\Support\Facades\Input;
class RegistrationController extends \BaseController {
public function __construct()
{
$this->beforeFilter('guest');
}
this controller is in global namespace. so you don't need to use use Illuminate\Support\Facades\Input; you can directly call Input::get('foo');
<?php namespace Foo; //<---- check the namespace
use Input;
class RegistrationController extends \BaseController {
public function __construct()
{
$this->beforeFilter('guest');
}
here you can write either, use Input or \Input::get('foo') while calling.
You don't have to use importing namespaces (you don't need to add use Illuminate\Support\Facades\Input;) here.
You can accesss Input facade, using Input::get('something') as long as your controller is in global namespace. Otherwise you need to use \Input::get('something') or add use Input after <?php.
I'm trying to set up PSR-4 within a new Laravel 4 application, but I'm getting some troubles achieving what I want when it comes to build controllers.
Here's what I have now :
namespace MyApp\Controllers\Domain;
class DomainController extends \BaseController {
public $layout = 'layouts.default';
public function home() {
$this->layout->content = \View::make('domain.home');
}
}
I'm not so fond of using \View, \Config, \Whatever to use Laravel's classes. So I was wondering if I could put a use Illuminate\View; to be able to use View::make without putting a \.
Unfortunately, while doing this, I'm getting the following error : Class 'Illuminate\View' not found.
Could somebody help with this please ?
The problem in your case is that View is not located in Illuminate namespace but in Illuminate\View namespace, so correct import would be not:
use Illuminate\View;
but
use Illuminate\View\View;
You can look at http://laravel.com/api/4.2/ to find out which namespace is correct for class you want to use
Assuming BaseController.php has a namespace of MyApp\Controllers\Domain
namespace MyApp\Controllers\Domain;
use View;
class DomainController extends BaseController {
public $layout = 'layouts.default';
public function home() {
$this->layout->content = View::make('domain.home');
}
}
If BaseController.php has other namespace, i.e MyApp\Controllers
namespace MyApp\Controllers\Domain;
use MyApp\Controllers\BaseController;
use View;
class DomainController extends BaseController {
public $layout = 'layouts.default';
public function home() {
$this->layout->content = View::make('domain.home');
}
}
If, for instance, you controller needs to use another base class from Laravel, lets say Config.
namespace MyApp\Controllers\Domain;
use MyApp\Controllers\BaseController;
use View;
use Config;
class DomainController extends BaseController {
public $layout = 'layouts.default';
public function home() {
$this->layout->content = View::make('domain.home')->withName(Config::get('site.name'));
}
}
The use of View::make() takes advantage of the Laravel facades. To properly reference the facade, instead of directly referencing the class that gets resolved out of the iOC container, I would use the following:
use Illuminate\Support\Facades\View;
This will reference the View facade that is being used when calling View::make()