How to get session in all pages.? codeigniter - mysql

Here I want to use session in my all pages any one can help me on this topic. some says create a custom controller in library, and/or use helper/hook, I don't know what will work.
If any one have a little example code and little desc. then please share here.
How can I use session in all pages.?

From the CodeIgniter documentation
Sessions will typically run globally with each page load, so the
Session class should either be initialized in your controller
constructors, or it can be auto-loaded by the system. For the most
part the session class will run unattended in the background, so
simply initializing the class will cause it to read, create, and
update sessions when necessary.
To initialize the Session class manually in your controller
constructor, use the $this->load->library() method:
$this->load->library('session');
So, in order to initialize the Session library in ALL your controllers, the best idea is to have a custom "general controller"
<?php
class MyController extends CI_Controller {
public function __construct()
{
$this->load->library('session');
}
}
and then have all your contollers extend not the default CI_Controller, but your "MyContoller", like in
<?php
class Blog extends MyController {
public function index()
{
echo 'Hello World!';
}
}
I hope this is what you need.

Related

Laravel 5.4: attach custom service provider to a controller

I created a service provider named AdminServiceProvider
namespace App\Providers;
use Modules\Orders\Models\Orders;
use Illuminate\Support\ServiceProvider;
use View;
class AdminServiceProvider extends ServiceProvider
{
public function boot()
{
$comments = Orders::get_new_comments();
View::share('comments', $comments);
}
public function register()
{
}
}
Registered the provider
App\Providers\AdminServiceProvider::class,
Now I try to attach it to the controller
namespace App\Http\Controllers\admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Providers\AdminServiceProvider;
class AdminController extends Controller
{
public $lang;
public function __construct()
{
}
public function index(){
return view('admin/dashboard');
}
}
Now I get this error message
Undefined variable: comments
This is the first time I try to use a custom service provider and don't know exactly how it works I'm sure there's something missing Hope you can help. Thanks in advance.
[UPDATE]
removed use App\Providers\AdminServiceProvider; from the controller
php artisan clear-compiled solved the problem but I want to attach it to some controllers not all controllers as the $comments are sent to all contollers in my app. So how to attach the service provider to specific controllers not all of them?
For the undefined variable run: php artisan clear-compiled will solve it
If you want to share a variable in some of your views you can create a middleware and assign it to the views you want to share the data with:
First create a middleware: php artisan make:middleware someName
Then in the handle function you add your view sharing logic:
$comments = Orders::get_new_comments();
view()->share('comments',$comments);
return $next($request);
Then register your middleware under the $routeMiddleware array and
give it an alias.
Then attach it to your routes like:
Route::group(['middleware'=> 'yourMiddlewwareName'], function(){
//your routes
});
If you have all your admin views in one directory (views\admin for example) you can use view composer in AdminServiceProvider:
public function boot()
{
view()->composer('admin.*', function($view){
$view->with('comments', Orders::get_new_comments());
});
}
It will attach comments variable to each view in your views\admin directory.
You can also attach a variable to some specific views or folders like this:
view()->composer(['admin.posts.*', 'admin.pages.index'], function($view){
$view->with('comments', Orders::get_new_comments());
});

PhpStorm does not support yaf's init method

In other class, PhpStorm can identify __construct() function, but in yaf controller it can not identify the initialization method init(), resulting in the init() can not trace initialization operation.
class TestController extends Yaf_Controller_Abstract{
private $model;
public function init() {
$this->model = new TestModel();
}
public function test(){
$this->model->testDeclaration();
}
}
class TestModel{
public function testDeclaration(){
}
}
In this example, I want use 'go to declaration' from test function $this->model->testDeclaration(); to testDeclaration() function in TestModel class. But it does not work.
PhpStorm tells me:
Cannot find declaration to go to
How can I use 'go to declaration' correctly here?
In other class, PhpStorm can identify __construct() function, but in yaf controller it can not identify the initialization method init(), resulting in the init() can not trace initialization operation.
PhpStorm has special treatment for __constructor() -- it tracks what type class variable/property will get if it will have any assignment operations within the method body.
For example, in this code it knows that $this->model will be instance of TestModel class -- IDE keeps this info even outside of the __construct() method body.
For other methods, like init() in your case, such info gets discarded outside (so it's local to the method body only).
You can easily resolve this by using simple PHPDoc comment with #var tag where you will provide type hint for model property:
/** #var \TestModel Optional description here */
private $model;
Make a habit of providing type hint for all properties/class variables, even if IDE autodetects its' type -- it helps IDE in long run.
https://phpdoc.org/docs/latest/references/phpdoc/tags/var.html

Call function in indexController from another controller Zend

Need to call some function from another controller (not IndexController), for example:
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
$someFunction = new CustomController();
$someFunction->someFunc();
}
}
But this throws an error :
Fatal error: Class 'CustomController' not found in C:\xampp\htdocs\demo.ru\application\controllers\IndexController.php on line 13
If YourController::memberFunction() does something that is needed across multiple controllers, put that code in a action helper or library class,
so that both controllers can access the shared functionality without having to depend on each other.
I would suggest you to follow DRY and move those functions to common library place.
to use with Namespace see
Zend Framework calling another Controller Action
hope this will sure help you.

Global MySQL query in Codeigniter

In an attempt to try and narrow my query down, I'm still very new to Codeigniter framework...
I want to define global variables (eg. in autoloaded helpers) and use global mysql queries throughout my site - but I don't understand how to do the latter (global mysql queries).
I understand the concept of defining single variables in a helper... and I understand the concept of creating a single mysql query in a model, loading it in a controller and using it in a view file (with a foreach loop).
How (and where) do I create a mysql query that can be autoloaded (or whatever) and used anywhere on my site - without the need to load it in every controller?
this might help.. models would probably be your best bet. below i have given some insite on how to use controlelrs/models/libraries in general, i would put my mysql code into the appropriate model file. and call it via any controller
// Library/profiles.php
class My_library
{
protected $CI;
public function __construct()
{
$this->CI =& get_instance(); // Existing Code Igniter Instance
}
public function my_lib_method()
{
// Your Code Here
// can communicate back with CI by using $this->CI
// $this->CI->load->view(....);
// $this->CI->load->model(...);
// ETC
}
}
// models/my_model.php
class My_model extends CI_Model{
public function my_mdl_method(){
// Your Code Here
}
}
// controllers/my_controller.php
class My_controller extends CI_Controller
{
public function my_ctrl_method(){
$this->load->library('my_library');
$this->load->model('my_model');
// calling a library method
$this->my_library->my_lib_method();
// calling a model method
$this->my_model->my_mdl_method();
}
}

Zend FlashMessenger problems

I am new to Zend framework and I have a problem.
I created a controller abstract class which implements the functions like:
protected function AddError($message) {
$flashMessenger = $this->_helper->FlashMessenger;
$flashMessenger->setNamespace('Errors');
$flashMessenger->addMessage($message);
$this->view->Errors = $flashMessenger->getMessages();
}
protected function activateErrors()
{
$flashMessenger = $this->_helper->FlashMessenger;
$flashMessenger->setNamespace('Errors');
$this->view->Errors = $flashMessenger->getMessages();
}
So for each controller I am able to use
$this->AddError($error);
And then I render $error in layout.
So I want not to deal with flashMesenger in every controller.
but I have to execute the activateErrors when each action is executed.
for example
I have an controller test
class TestController extends MyController {
public function indexAction() {
$this->AddError("Error 1");
$this->AddError("Error 2");
$this->activateErrors();
}
public function index1Action() {
$this->AddError("Esdsd 1");
$this->AddError("sddsd 2");
$this->activateErrors();
}
}
Is there a way that I could execute this activateErrors in each action for every controller at the end of action without duplicating the code.
I mean I do not want to include this code at every action. Maybe there is a way to include it in my abstract class MyController.
Anybody any Idea?
thanks
What about using a postDispatch hook, in your parent MyController ?
Quoting that page :
Zend_Controller_Action specifies two
methods that may be called to bookend
a requested action, preDispatch() and
postDispatch(). These can be useful in
a variety of ways: verifying
authentication and ACL's prior to
running an action (by calling
_forward() in preDispatch(), the action will be skipped), for instance,
or placing generated content in a
sitewide template (postDispatch()).
Maybe this might do the trick ?
I actually contributed an enhancement to FlashMessenger which provides a lot of the functionality you're looking for.