Codeigniter 4🔥 [27.- Sesiones]
En este tutorial vamos a trabajar con las sesiones de codeigniter 4, a esta alturas ya todos debemos saber que es una sesión y en particular que es una sesion en PHP, en codeigniter 4 es exactamente lo mismo pero este framework nos da la oportunidad de trabajar de manera muy practica y facil.
Codigo del tutorial: https://github.com/programadornovato/codeigniter4/commit/454a50aa8fbf86bdd8763c2bdf9db5fe71d62675
//app/Controllers/BaseController.php <?php namespace App\Controllers; /** * Class BaseController * * BaseController provides a convenient place for loading components * and performing functions that are needed by all your controllers. * Extend this class in any new controllers: * class Home extends BaseController * * For security be sure to declare any new methods as protected or private. * * @package CodeIgniter */ use CodeIgniter\Controller; class BaseController extends Controller { /** * An array of helpers to be loaded automatically upon * class instantiation. These helpers will be available * to all other controllers that extend BaseController. * * @var array */ protected $helpers = [ ]; //public $session=null; /** * Constructor. * */ public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger) { // Do Not Edit This Line parent::initController($request, $response, $logger); helper('operaciones'); //$this->session= \Config\Services::session(); //-------------------------------------------------------------------- // Preload any models, libraries, etc, here. //-------------------------------------------------------------------- // E.g.: // $this->session = \Config\Services::session(); } } //app/Controllers/Micontrolador.php <?php namespace App\Controllers; use CodeIgniter\Controller; use App\Models\UserModel; class Micontrolador extends BaseController { public $session=null; public function __construct(){ helper('form'); $this->session= \Config\Services::session(); } public function ponerDatos(){ $newdata=[ 'name'=>'novato', 'email'=>'[email protected]', 'login'=>TRUE, ]; $this->session->set($newdata); echo $this->session->get('email'); } public function leerDatos(){ if($this->session->has('name')){ echo "name=".$this->session->get('name')."<br>"; echo "email=".$this->session->get('email')."<br>"; echo "login=".$this->session->get('login')."<br>"; } else{ echo "No hay datos"; } } public function quitarDatos(){ $this->session->remove('email'); } public function destruirDatos(){ $this->session->destroy(); } public function guarda(){ $userModel=new UserModel($db); $request= \Config\Services::request(); $data=array( 'name'=>$request->getPostGet('name'), 'email'=>$request->getPostGet('email'), ); if($request->getPostGet('id')){ $data['id']=$request->getPostGet('id'); } if($userModel->save($data)===false){ var_dump($userModel->errors()); } if($request->getPostGet('id')){ $users=$userModel->find([$request->getPostGet('id')]); $users=array('users'=>$users); $estructura=view('estructura/header').view('estructura/formulario',$users); } else{ $users=$userModel->findAll(); $users=array('users'=>$users); $estructura=view('estructura/header').view('estructura/body',$users); } return $estructura; } public function editar(){ $userModel=new UserModel($db); $request= \Config\Services::request(); if($request->getPostGet('id')){ $id=$request->getPostGet('id'); }else{ $id=$request->uri->getSegment(3); } $users=$userModel->find([$id]); $users=array('users'=>$users); $estructura=view('estructura/header').view('estructura/formulario',$users); return $estructura; } public function borrar(){ $userModel=new UserModel($db); $request= \Config\Services::request(); if($request->getPostGet('id')){ $id=$request->getPostGet('id'); }else{ $id=$request->uri->getSegment(3); } $userModel->delete($id); $users=$userModel->findAll(); $users=array('users'=>$users); $estructura=view('estructura/header').view('estructura/body',$users); return $estructura; } public function formulario(){ $estructura=view('estructura/header').view('estructura/formulario'); return $estructura; } public function index() { $userModel=new UserModel($db); $datos=$userModel->paginate(10); $paginador=$userModel->pager; $paginador->setPath('ci4/'); $datos=array('users'=>$datos,'paginador'=>$paginador); $estructura=view('estructura/header').view('estructura/body',$datos); return $estructura; } public function imagenManipulacion(){ $info=\Config\Services::image() ->withFile('codeigniter.jpg') ->getFile() ->getProperties(true); $ancho=$info['width']; $alto=$info['height']; $imagen=\Config\Services::image() ->withFile('codeigniter.jpg') ->reorient() //->rotate(90) //->fit(250,250,'bottom-left') //->resize($ancho/2,$alto/2) ->crop(300,300,50,0) ->save('codeigniter_p.jpg'); return view('estructura/imagen'); } //-------------------------------------------------------------------- }
Guia oficial codeigniter: https://codeigniter4.github.io/userguide/libraries/sessions.html
#Codeigniter4 #php7 #backend #Codeigniter #php .
? Esta lista de reproducción: https://www.youtube.com/playlist?list=PLCTD_CpMeEKTFN5TDeOP-wP_hU0_9VoWg
.
Codigos en gdrive: https://drive.google.com/file/d/1awbG6jI40IO0N68fgjLUwNFpsTC3wIHj/view?usp=sharing .
Gracias por apoyar este canal: https://www.patreon.com/programadornovato?fan_landing=true .
? Facebook: https://facebook.com/ProgramadorNovatoOficial
? Twitter: https://twitter.com/programadornova
? Linkedin: https://www.linkedin.com/in/programadornovato/
? Instagram: https://www.instagram.com/programadornovato/