I have a controller called UsersController, is there a way I can output some values from this controller to the cakephp default layout?
Here is what I have tried, the default.ctp:
<?php foreach ($users as $user): ?>
<ul class="nav navbar-nav navbar-right">
<?php if (!$this->Session->read('Auth.User.id')) : ?>
<li>
<?php echo $this->Html->link('Login',array('controller' => 'us 'action' => 'login', 'full_base' =>'true)); ?></li>
<li>
<?php echo $this->Html->link('Register', array('controller' => 'users', 'action' => 'add', 'full_base' => true)); ?>
</li>
<?php else: ?>
<li class="dropdown">
<a href="<?php echo $this->Html->url(array('controller' => 'users', 'action' => 'edit', 'full_base' => true)); ?>" class="dropdown-toggle" data-toggle="dropdown">
<?php if(empty($user[ 'User'][ 'filename'])){ ?>
<i class="fa fa-user fa-lg"></i>
<?php }else{ echo $this->Html->image($user['User']['filename'], array('alt' => $user['User']['username'],'class'=>'img-responsive img-rounded','width'=>'32','height'=>'32','style'=>'display:inline; background:red;')); } ?>
<?php $users[ 'User'][ 'username']; ?> <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<i class="fa fa-gear fa-lg"></i> Settings
</li>
<li>
<i class="fa fa-power-off fa-lg"></i> Logout
</li>
<li>Something else here
</li>
<li class="divider"></li>
<li>Separated link
</li>
</ul>
</li>
<?php endif; ?>
</ul>
<?php endforeach; ?>
And here is my AppController.php:
public function beforeFilter(){
return $this->getuser();
}
public function getuser($id = null){
$this->loadModel('User');
$user = $this->User->find('all', array(
'conditions' => array('User.id' => $id)
));
$this->set('users',$user);
}
The problem with this code is that,it keeps returning the following notice,
Notice (8): Undefined index: User [APP\View\Layouts\default.ctp, line
71]
please how do i resolve this issue?
If you want all fields from logged user, you can use this in your view.
$user = $this->Session->read('Auth.User'));
This will return all storage data from users ( username, filename, all fields from user table)
Just change in your controller you have used
public function getuser($id = null){
insted of this use
public function beforeRender($id = null) {
Your AppController.php code should look like this
public function beforeFilter(){
return $this->getuser();
}
public function beforeRender($id = null){
$this->loadModel('User');
$user = $this->User->find('all', array(
'conditions' => array('User.id' => $id)
));
$this->set('users',$user);
}
Related
Route report.expAlert error
(View: /home/grocersa/edesimarket.com/resources/views/layout/main.blade.php
(i defined all place but not triggered plz help to find out)
This is my web.php file coding
Route::get('report/product_quantity_alert', 'ReportController#productQuantityAlert')->name('report.qtyAlert');
Route::get('report/product_expiry_alert', 'ReportController#productExpiryAlert')->name('report.expAlert');
Route::get('report/warehouse_stock', 'ReportController#warehouseStock')->name('report.warehouseStock');
This my main.blade.php coding
for sidebar (nav - menu )
#if($product_exp_alert_active)
<li id="expAlert-report-menu">
{{trans('file.Product Expiry Alert')}}
</li>
#endif
for user permission
#if($product_qty_alert_active || $product_exp_alert_active)
for database & roll permission
$product_qty_alert_active = DB::table('permissions')
->join('role_has_permissions', 'permissions.id', '=', 'role_has_permissions.permission_id')
->where([
['permissions.name', 'product-qty-alert'],
['role_id', $role->id] ])->first();
$product_exp_alert_active = DB::table('permissions')
->join('role_has_permissions', 'permissions.id', '=', 'role_has_permissions.permission_id')
->where([
['permissions.name', 'product-exp-alert'],
['role_id', $role->id] ])->first();
for define
#if($alert_product > 0)
<li class="nav-item">
<a rel="nofollow" data-target="#" href="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" class="nav-link dropdown-item"><i class="dripicons-bell"></i><span class="badge badge-danger">{{$alert_product}}</span>
</a>
<ul class="dropdown-menu edit-options dropdown-menu-right dropdown-default notifications" user="menu">
<li class="notifications">
{{$alert_product}} product exceeds alert quantity
</li>
<li class="notifications">
{{$alert_product}} product expiry alert
</li>
</ul>
</li>
#endif
This is my reportcontroller.php coding
class ReportController extends Controller
{
public function productQuantityAlert()
{
$role = Role::find(Auth::user()->role_id);
if($role->hasPermissionTo('product-qty-alert')){
$lims_product_data = Product::select('name','code', 'image', 'qty', 'alert_quantity')->where('is_active', true)->whereColumn('alert_quantity', '>', 'qty')->get();
return view('report.qty_alert_report', compact('lims_product_data'));
}
else
return redirect()->back()->with('not_permitted', 'Sorry! You are not allowed to access this module');
}
public function productExpiryAlert()
{
$role = Role::find(Auth::user()->role_id);
if($role->hasPermissionTo('product-exp-alert')){
$lims_product_data = Product::select('name','code', 'image', 'qty', 'alert_expiry')->where('is_active', true)->whereColumn('alert_expiry', '>', 'name')->get();
return view('report.exp_alert_report', compact('lims_product_data'));
}
else
return redirect()->back()->with('not_permitted', 'Sorry! You are not allowed to access this module');
}
By using view composer to pass variables i got the error like this:
"Trying to get property 'sub_cat_name' of non-object (View: D:\xampp\htdocs\E-commerce\resources\views\layouts\includes\top.blade.php)". please resolve the proble the code is given below:
AppServiceProvider:
View::composer(['layouts.includes.top'], function ($view) {
$view->with('subcat', SubCategory::get());
});
controller:
public function prdtview()
{
$categories = Category::get();
$data=DB::select('select category.cat_name,product_images.prdt_image,product.prdt_name,product.actual_price from product INNER JOIN category on product.catid = category.id INNER JOIN product_images on product.id = product_images.prdt_id where product.sub_cat_id = 3 ');
return view("frontend.product",[
'data' => $data,
'category' =>$categories
]);
}
blade file:
<div class="nav-item">
<div class="container">
<nav class="nav-menu mobile-menu">
<ul>
<li class="active">Home</li>
#foreach($category as $category)
<li>{{$category->cat_name}}
<ul class="dropdown">
#foreach($subcat as $subcat)
<li><?php echo $subcat->sub_cat_name; ?></li>
#endforeach
</ul>
</li>
#endforeach
</ul>
</nav>
<div id="mobile-menu-wrap"></div>
</div>
</div>
change it:
#foreach($subcat as $subcat)
<li><?php echo $subcat->sub_cat_name; ?></li>
#endforeach
to some thing like :
#foreach($subcat as $sub_cat)
<li><?php echo $sub_cat->sub_cat_name; ?></li>
#endforeach
how can I embed a foreach with my header?
I have already tried as in the code.
But with the output I get only "Undefined variable: header_warenkorbs" raus.
Although I am involved in the controller with the value data.
What am I doing wrong?
Controller:
<?php
class Warenkorb extends CI_Controller
{
function __construct()
{
parent::__construct();
}
public function header()
{
$data['header_warenkorbs'] = $this->Admin_model->header_warenkorbs();
$this->load->view('templates/header', $data);
// $this->load->view('test/index');
// $this->load->view('templates/footer');
}
}
Model:
<?php
class Warenkorb_model extends CI_Model
{
public function header_warenkorbs()
{
$this->db->select('*');
$this->db->from('db_artikel');
$query = $this->db->get();
return $query->result_array();
}
}
View:
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<span class="glyphicon glyphicon-shopping-cart"></span><span class="cart-items-count"><span class=" notification-counter">243</span></span>
<ul class="dropdown-menu dropdown-cart" role="menu">
<?php foreach($header_warenkorbs as $warenkorb): ?>
<li>
<span class="item">
<span class="item-left">
<span class="item-info">
<span>Item name</span>
<span>23$</span>
</span>
</span>
<span class="item-right">
<button class="btn btn-xs btn-danger pull-right">x</button>
</span>
</span>
</li>
<?php endforeach; ?>
<li class="divider"></li>
<li><a class="text-center" href="<?php echo base_url(); ?>warenkorb/index">Warenkorb</a></li>
</ul>
</li>
</ul>
Unless it is a typo or you're not showing us something the problem could be with this line.
$data['header_warenkorbs'] = $this->Admin_model->header_warenkorbs();
The model code you show is Warenkorb_model not Admin_model. Seems like the above code should maybe be
$data['header_warenkorbs'] = $this->Warenkorb_model->header_warenkorbs();
You don't show where you load the model. It is loaded somewhere... right? It is common practice to load models in the constructor of the Controller that uses the model.
class Warenkorb extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('Warenkorb_model');
}
// other code
}
Do you realize you are not actually doing anything with the $header_warenkorbs in your foreach loop? You never use $warenkorb.
you need to echo it (example:)
<?php
echo '<table>
<tr>
<td>Name</td>
<td> .$name.</td>
</tr>
</table>';
?>
just edit it to your needs, im too lazy
I am working on a Codeigniter 3 authentication application. I am new to CI, so the problems are run into are "rookie" level.
The newest one is: if a user has signed in, even though he/she is redirected to the members area, the signed in state does not persist. Navigating to the home page (or on any another page) signs the user out.
Once the user is logged in, I don't want the login form page to be accessible.
The sessions library is globally loaded.
The user model is:
class Usermodel extends CI_Model {
public function user_login($email, $password)
{
$query = $this->db->get_where('users', array('email' => $email, 'password' => $password));
return $query->row();
}
}
The controller looks like this:
class Signin extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('signin');
}
public function signin()
{
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
if ($this->form_validation->run())
{
$email = $this->input->post('email');
$password = $this->input->post('password');
$this->load->model('Usermodel');
$current_user = $this->Usermodel->user_login($email, $password);
if ($current_user) {
$current_user_id = $this->session->userdata('user_id', $current_user->id);
$current_user_email = $this->session->userdata('email', $current_user->email);
redirect('home');
} else {
$this->session->set_flashdata("signin_failure", "Incorrect email or password");
$this->load->view('signin');
}
}
else
{
$this->load->view('signin');
}
}
}
The login form view:
<?php echo form_open('Signin/signin'); ?>
<div class="form-group <?php if(form_error('email')) echo 'has-error';?>">
<?php echo form_input(array(
'name' => 'email',
'id' => 'email',
'class' => 'form-control',
'autocomplete' => 'off',
'placeholder' => 'Email address'
));
if(form_error('email')) echo '<span class="glyphicon glyphicon-remove"></span>';
echo form_error('email'); ?>
</div>
<div class="form-group <?php if(form_error('password')) echo 'has-error';?>">
<?php echo form_password(array(
'name' => 'password',
'id' => 'password',
'class' => 'form-control',
'autocomplete' => 'off',
'placeholder' => 'Password'
));
if(form_error('password')) echo '<span class="glyphicon glyphicon-remove"></span>';
echo form_error('password'); ?>
</div>
<div class="form-group">
<?php echo form_submit(array(
'name' => 'login',
'value' => 'Login',
'class' => 'btn btn-success btn-block'
)); ?>
</div>
<?php echo form_close(); ?>
Why is the loggedin state not kept? Thank you!
UPDATE
User Helper:
function is_logged_in() {
$CI =& get_instance();
$user = $CI->session->userdata('user_data');
return isset($user);
}
The navigation bar code:
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav navbar-right">
<?php if(is_logged_in()) : ?>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#"><span class="glyphicon glyphicon-user"></span> Hello, user
<span class="caret"></span></a>
</a>
<ul class="dropdown-menu">
<li>My contacts</li>
<li>All contacts</li>
<li>Signout</li>
</ul>
</li>
<?php else: ?>
<li><span class="glyphicon glyphicon-log-in"></span> Login</li>
<li><span class="glyphicon glyphicon-user"></span> Register</li>
<?php endif;?>
</ul>
</div>
Just after validating whether username and password matches, you have to set data in session, for that you need, small correction:
// if found in table, then save it session
// so that it can be accessible from elsewhere
if ($current_user) {
$this->session->set_userdata(
array(
'user_id' => $current_user->id
'email'=> $current_user->email
)
);
}
To check whether user logged in or not in all other controller, best way is to create helper, and call helper function to validate logged in user.
So for that in helpers directory, say you create file user_helper.php, with below code
<?php defined('BASEPATH') OR exit('No direct script access allowed');
function is_logged_in() {
$CI =& get_instance();
$user = $CI->session->userdata('user_data');
return isset($user);
}
Open file config/autoload.php and add your helper so that in all other controller you can it easily:
$autoload['helper'] = array('user');
So in any controller you can easily check whether user logged in or not like below
if(is_logged_in()){
// proceed authenticated
}else{
// redirect to signin page or somewhere else as per your wish
redirect('signin');
}
You said :
Once the user is logged in, I don't want the login form page to be
accessible.
So you need
public function signin()
{
if(is_logged_in()){
$this->session->set_flashdata('message_name', 'You already logged In');
//redirect to some function
redirect("controller/function_name");
}
// if not logged in then display form
// your other code goes here..
}
and in your view file where you redirect
echo $this->session->flashdata('message_name');
You Need to set userdata first.
change
$current_user_id = $this->session->userdata('user_id', $current_user->id);
$current_user_email = $this->session->userdata('email', $current_user->email);
to
$current_user_id = $this->session->set_userdata('user_id', $current_user->id);
$current_user_email = $this->session->set_userdata('email', $current_user->email);
I am trying to pass the page id through the url Here is my code,problem is i will not get the required page id to the page only get the path . Please help.
View Page
<li>
<i class="fa fa-table fa-fw"></i>Hotel 1
</li>
<li>
<i class="fa fa-table fa-fw"></i>Hotel 2
</li>
Controller Page
public function view()
{
echo $page_id =$this->uri->segment(1);
$this->load->helper('url');
$data['offers_name'] = $this->Login_set->get_offer($id);
$data['offers_description'] = $data['offers_name']['offers_description'];
$this->load->view('App_stay/pages/hotel1_offers.php');
}
public function view1()
{
echo $page_id =$this->uri->segment(2);
$this->load->helper('url');
$data['offers_name'] = $this->Login_set->get_offer($id);
$data['offers_description'] = $data['offers_name']['offers_description'];
$this->load->view('App_stay/pages/hotel2_offers.php');
}
Change this:
<li>
<i class="fa fa-table fa-fw"></i>Hotel 1
</li>
<li>
<i class="fa fa-table fa-fw"></i>Hotel 2
</li>
into this:
<li>
<i class="fa fa-table fa-fw"></i>Hotel 1
</li>
<li>
<i class="fa fa-table fa-fw"></i>Hotel 2
</li>
Add $this->uri->segment('segment_number') in your controller to get the value from url
public function view()
{
$page_id =$this->uri->segment(3);
$data['offers_name'] = $this->Login_set->get_offer($id);
$data['offers_description'] = $data['offers_name']['offers_description'];
$this->load->view('App_stay/pages/hotel1_offers.php');
//I put your codes here because you want Spoon Feed
}
public function view1()
{
$page_id =$this->uri->segment(3);
...//rest of your codes here
}
You can pass as url parameter
<li>
<i class="fa fa-table fa-fw"></i>Hotel 1
</li>
And can access like this in controller
public function view($page_id)
{
echo $page_id;
}
you can load helper in controller constructor
function __construct() {
$this->load->helper('url');
}