How to use session in custom class laravel 5? - php

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

Related

How to create global variable in .tpl file in PrestaShop?

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();
}
}
?>

How to solve namespaces problems in php tinker?

I have the following code:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Enemy extends Model
{
// ...
static function fight($id)
{
if(Enemy::calcDist($id))
{
$model = Enemy::find($id);
if($model->status == 1)
{
$model->status = 2;
$model->save();
}
}
}
}
When I try to do App\Enemy::fight(1) in php tinker it shows error:
"Class 'App\App\Enemy' not found".
I tried with "calcDist($id)", with "self::calcDist($id)", also at find($id) function, but no result.
How I can solve this?
Edit: I found the problem; that error comes from another part of code...
When you are in namespace App you dont need to use App\Enemy in your call.
Simply use Enemy::fight(1), or use the absolute namespace \App\Enemy::fight(1)
When you use a static class by his name, the engine search the class into the current namespace. If no namespace is given, then it uses the namespace "\".
namespace App;
Enemy::fight(1); // \App\Enemy::fight(1) ok
App\Enemy::fight(1); // \App\App\Enemy::fight(1) wrong

Laravel session value is not showing in app/Hlepers/common_helpers.php and the base controller

I am a beginner in laravel and developing an application in Laravel 5.3. I created one common_helper.php file in app/Helpers directory and created a service provider and added it in config/app.php.
I tried to call a function located in the common helper from my controller. It is going to the helper file. But Not showing the session value in the common helper, which is available in the controller.
I already did something in laravel 5.2. But there the same structure working perfectly
What may be the issue? I couldn't figure out it. Please help
common_helper.php
<?php
use App\User;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Route;
use App\Http\Requests;
use App\Menu_master;
use Illuminate\Support\Facades\Input;
use Intervention\Image\Facades\Image as Image;
function check_session()
{
echo Session::get('email');die;
if (Session::has('email') && Session::has('login') && Session::has('role_id') && Session::has('role_name'))
{
if (empty(Session::get('email')) || Session::get('login') !== 'true' || empty(Session::get('role_id')) || empty(Session::get('role_name')))
{
Session::flush();
Redirect::to('/')->send();
exit(0);
}
}
else
{
Session::flush();
Redirect::to('/')->send();
exit(0);
}
}
I am calling the check_session() from the controller construct function like below
public function __construct(){
check_session();
}
You should use the session's namespace first.
use Session;
//or
use Illuminate\Support\Facades\Session;

laravel passing value from controller to model

I have a base controller that gets certain default properties based on the domain name for every request. These include app_id and language_id.
What would be the recommended way to get these values set in my models, so that for example if I tried to create a new Post model it would have the app_id and language_id set by the values defined in my base_controller.
Kind regards to any response.
You can use sessions to store your app_id and language_id, and get it from session whenever you need it.
Check How to use sessions in Laravel from here
You can use __consruct method as suggested in comment.
Here is another alternative:-
YourController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\YourModel;
class YourController extends Controller
{
public function callModelStaticMethod()
{
$data = ['appId'=>4, 'languageId'=>5];
YourModel::passDataToModelMethod($data);
}
}
YourModel.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class YourModel extends Model
{
public static function passDataToModelMethod($data)
{
var_dump($data);
// Write your business logic here.
// $newObj = new self;
// $newObj->app_id = $data['app_id'];
// $newObj->language_id = $data['languageId'];
// $newObj->save();
}
}
#ravi...just bit correction to your code. you can't call directly your method from model.
YourModel::passDataToModelMethod($data);
above won't work, you need to do as follows
$insertData=new YourModel();
$insertData->passDataToModelMethod($data);

Laravel Illuminate\Support\Facades\Input

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.

Categories