Codeigniter 4🔥 [23.- Vistas anidadas]
En este tutorial vamos a trabajar con vistas anidadas y si se preguntan que es eso de una vista anidada pues es tan simple como colocar una vista dentro de otra 🙂
Codigo en github.com: https://github.com/programadornovato/codeigniter4/commit/7f92ab19f47adc0ac49c0a3b7fce32f7ca3753c5
//app/Controllers/Micontrolador.php <?php namespace App\Controllers; use CodeIgniter\Controller; use App\Models\UserModel; class Micontrolador extends BaseController { public function __construct(){ helper('form'); } 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->findAll(); $datos=array('users'=>$datos,'cabecera'=>view('estructura/header')); $estructura=view('estructura/body',$datos); return $estructura; } //-------------------------------------------------------------------- } //app/Views/estructura/body.php <?php echo $cabecera; //echo view('estructura/header'); ?> <body> <div class="container"> <div class="row"> <a href="<?php echo base_url(); ?>nuevo" class="btn btn-info" role="button" >Nuevo</a> </div> <div class="row"> <table class="table"> <tr> <th scope="col">id</th> <th scope="col">name</th> <th scope="col">email</th> <th scope="col">deleted</th> <th scope="col">acciones</th> </tr> <?php foreach($users as $user){ echo "<tr scope='row' >"; echo "<td>".$user['id']."</td>"; echo "<td>".$user['name']."</td>"; echo "<td>".$user['email']."</td>"; echo "<td>".$user['deleted']."</td>"; echo "<td>"; ?> <a href="<?php echo base_url(); ?>micontrolador/editar/<?php echo $user['id']; ?>" class="btn btn-warning" role="button" ><i class="fa fa-pencil-square-o"></i></a> <a href="<?php echo base_url(); ?>micontrolador/borrar/<?php echo $user['id']; ?>" class="btn btn-danger" role="button" ><i class="fa fa-trash"></i></a> <?php echo "</td>"; echo "</tr>"; } ?> </table> </div> </div> </body> </html>
Guia oficial: https://codeigniter4.github.io/userguide/outgoing/views.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/