laravel 5.4 controller middleware construct not working - laravel-5.4

I have some code :
AController
class AController extends Controller
{
public function __construct()
{
echo 'outside - AController';
echo '<br/>';
$this->middleware(function ($request, $next) {
echo 'inside - AController';
echo '<br/>';
return $next($request);
});
}
public function handle()
{
$B = new BController;
if ( $B->check() ) {
$controller = BController::class;
$action = 'index';
} else {
$controller = BController::class;
$action = 'nothing';
}
$container = app();
$route = $container->make(Route::class);
$controllerInstance = $container->make($controller);
return (new ControllerDispatcher($container))->dispatch($route, $controllerInstance, $action);
}
}
BController
class BController extends Controller
{
public function __construct()
{
echo 'outside - BController';
echo '<br/>';
$this->middleware(function ($request, $next) {
echo 'inside - BController';
echo '<br/>';
return $next($request);
});
}
}
When BController is called from AController, in the browser show :
outside - AController
inside - AController
outside - BController
why middleware in BController when called from AController not showing like this?
outside - AController
inside - AController
outside - BController
inside - BController

use Middleware in route instead of using construct in controller.
here is the laravel docs https://laravel.com/docs/5.5/middleware

Related

Modules config file

I'm my api module's module.php file, i added this
/**
* {#inheritdoc}
*/
public function init()
{
parent::init();
// custom initialization code goes here
\Yii::configure($this, require __DIR__ . '/config/main.php');
}
in my controller i have this
public function actionIndex()
{
dd(Yii::$app->getModule('payment')->params['data']);
}
in my modules `main.php i have this
$params = ['data' => [ ... ]];
if (YII_ENV == 'dev') {
if (YII_ENV == 'dev') {
$params = array_merge(
require __DIR__ . '/../../../../common/config/params.php',
require __DIR__ . '/../../../../common/config/params-local.php',
require __DIR__ . '/params.php',
);
} else {
$params = array_merge(
require __DIR__ . '/../../../../common/config/params.php',
require __DIR__ . '/params.php',
);
}
return [
'params' => $params,
];
I'm getting this error Undefined array key "data" Any one know what im missing here, i'm trying to create a module config. Thank you.
There is not a main.php in module (there is in app/config).
in each module there is a Module.php class in modules/your_module scaffolding
If you need a specific $params array for the a module you should declare the params var
and assign your values or in declaration or in init() function
class Module extends \yii\base\Module
{
public $params = [];
.....
......
/**
* {#inheritdoc}
*/
public function init()
{
parent::init();
// custom initialization code goes here
$params['data'] = [......];
}

Can I store a repeating sql query separately and call them inside a function in model - Codeigniter

My current model is somewhat like
class select extends CI_Model{
function __construct(){
parent::__construct();}
public function get_a(){
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2','table1.id =
table2.id');
$this->db->where('x',1);
}
public function get_b(){
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2','table1.id =
table2.id');
$this->db->where('x',2);
}
public function get_c(){
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2','table1.id =
table2.id');
$this->db->where('x',3);
}
As you can see the same Join operation is repeating in all the three functions. Is there a way which I can just do this join once and use it with the where clause
And there are other questions. If that's possible is it efficient than this current method?
Repeating code is wasteful and avoidable. Here is one function that takes an argument to be used with where.
public function get_x($where){
$this->db
->select('*')
->from('table1')
->join('table2','table1.id = table2.id')
->where('x',$where);
}
if i understand you correct - you can do the following
class select extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function get_a()
{
$this->initializeBaseQuery();
$this->db->where('x',1);
}
public function get_b()
{
$this->initializeBaseQuery();
$this->db->where('x',2);
}
public function get_c()
{
$this->initializeBaseQuery();
$this->db->where('x',3);
}
private function initializeBaseQuery()
{
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2','table1.id = table2.id');
}
}
You can try this solution for your problem :
Select modal file :
<?php
class Select extends CI_Model{
function __construct(){
parent::__construct();
}
public function get_selection($where = array()){
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2','table1.id = table2.id', 'left');
if(!empty($where)) {
foreach ($where as $key => $value) {
$this->db->where($key,$value);
}
}
$query_result = $this->db->get();
$result = $query_result->result();
return $result;
}
}
?>
Text Controller file :
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Test extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function get_select(){
$where = array("x"=>"1"); // you can pass more than one key and value for condition.
$result =$this->select->get_selection($where);
echo "<pre>";
print_r($result);
exit;
}
}
}
?>
I Hope it will help you.

How to pass a variable from another function to the view in index function

I want to pass the $data variable from the findCity() function with the json return to the view in the index function.
How can I do that please ?
class HomeController extends Controller
{
public function index(Request $request)
{
$countries = Location::select('country_name')
->groupBy('country_name', 'country_name')
->get();
$cities = Location::select('city_name','city_name')
->where('region_name', $request->region_name)
->groupBy('city_name', 'city_name')
->get();
$areas = Area::get()->toTree();
return view('home', compact('areas','countries', 'cities'));
}
public function findCity(Request $request)
{
$data = Location::select('city_name','city_name')
->where('region_name', $request->region_name)
->groupBy('city_name', 'city_name')
->get();
return response()->json($data);
}
}
The Ajax code :
$(document).on('change','.regionName',function(){
//console.log("hmm its change");
var cat_id=$(this).val();
//console.log(cat_id);
var div=$(this).parent();
var op=" ";
$.ajax({
type:'get',
url:'{!!URL::to('findCityName')!!}',
data:{'region_name':cat_id},
success:function(data){
//console.log('success');
//console.log(data);
//console.log(data.length);
op+='<option value="0" selected disabled>Choose City</option>';
for(var i=0;i<data.length;i++){
op+='<option value="{{ route('user.location.store', $cities) }}">'+data[i].city_name+'</option>';
}
div.find('.cityName').html(" ");
div.find('.cityName').append(op);
},
error:function(){
}
});
});
});
You can make findCity() to be an static method and call it from inside the index() method:
public function index(Request $request)
{
// The rest of your code here...
// Calling the static method
$city = self::findCity($request);
return view('home', compact('areas', 'countries', 'cities', 'city'));
}
public static function findCity(Request $request)
{
$data = Location::select('city_name', 'city_name')
->where('region_name', $request->region_name)
->groupBy('city_name', 'city_name')->get();
return $data;
}
Read more about static methods in the PHP documentation.
You need to call,pass and return using a function. like this one. I think it can help you.
class HomeController extends Controller
{
public function index(Request $request)
{
$countries = Location::select('country_name')
->groupBy('country_name', 'country_name')
->get();
$cities = $this->findCity($request)
$areas = Area::get()->toTree();
return view('home', compact('areas','countries', 'cities'));
}
public function findCity($request)
{
$data = Location::select('city_name','city_name')
->where('region_name', $request->region_name)
->groupBy('city_name', 'city_name')
->get();
return response()->json($data);
}
}

reset password in codeigniter

i am trying to change password in database but it's not working..it printing the password correctly..but it's not save in the database..
Here is my controller:
public function resetpwd($user_id=NULL)
{
//echo $user_id;
$this->load->helper('form');
$data['msg']=array();
if($this->input->post())
{
$user_id= $this->input->post('user_id');
//echo $id;
$this->LoginModel->resetpwd($this->input->post(),$user_id);
//redirect(base_url('resetpwd/'.$id));
}
$this->load->view('Admin/resetpwd',$data);
}
Here is my Model:
function resetpwd($post='',$user_id)
{
$data=array('password'=>$post['password']);
$this->db->where('user_id', $user_id);
$this->db->update('users', $data);
return true;
print_r($data);
}
Please help me how to do this
Thank you
Please try this code.
function resetpwd($post=array(),$user_id)
You had passed post array to resetpwd function it is mendatory to get array into array variable.
Change only one line it will be working.
Thank You.
You can simply get the password in Model page using post method extract($_POST) and you can get your new password like $data=array('password'=>$password)
Your Model is-
function resetpwd($user_id)
{
extract($_POST);
$data=array('password'=>$password);
$this->db->where('user_id', $user_id);
$this->db->update('users', $data);
return true;
//print_r($data);
}
please try this code.
Controller :
class Adminuser extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('loginModel');
$this->load->helper('form');
}
public function resetpwd()
{
$data['msg']=array();
if($this->input->post())
{
$user_id= $this->input->post('user_id');
$this->loginModel->change_password($this->input->post('password'),$user_id);
}
$this->load->view('Admin/resetpwd',$data);
}
}
Your Model Function :
public function change_password($password,$user_id)
{
$data=array('password'=>$password);
$this->db->where('user_id', $user_id);
if($this->db->update('users', $data))
{
return true;
}
else{
return false;
}
}
Apply this code
function resetpwd($post, $user_id)
{
$data=array('password'=>$post['password']);
$this->db->where('user_id', $user_id);
$this->db->update('users', $data);
return true;
}

How to get value column where column in codeigniter 3

I have a table like:
sysopt|sysval
......................
site_url|http://domain.com/
site_title|My Website
......................
in mysql i use:
$query = $db->query("SELECT * FROM sysconfig");
while ($result = $db->fetch_array($query)) {
$settings[$result['sysopt']] = $result['sysval'];
}
But in CI:
class Sysinfo
{
var $info = array();
public function __construct()
{
$CI =& get_instance();
$settings = $CI->db->select("*")
->get("sysconfig");
foreach($settings as $setting) {
$this->info[$setting['sysopt']] = $setting['sysval'];
}
}
In view i call:
<?php echo $this->Sysinfo->info->site_url; ?>
Show error.
Message: Undefined property: CI_Loader::$Sysinfo
Thankyou any solution fix.
Why just not extend the CI_Model? For me, in framework..just extend the class so you would be easier
In your model:
class Sysinfo extends CI_Model
{
var $info = array();
public function __construct()
{
//$CI =& get_instance();
$settings = $this->db->select("*")
->get("sysconfig")->result_array(); //Return result as array
foreach($settings as $setting) {
$this->info[$setting['sysopt']] = $setting['sysval'];
}
}
function get_sysinfo_data()
{
return $this->info;
}
}
In your Controller
public function __construct()
{
$this->load->model('Sysinfo');
}
function index()
{
$data['arr_result'] = $this->Sysinfo->get_sysinfo_data();
$this->load->view('your_view',$data);
}
In view:
foreach($arr_result as $row_array):
var_dump($row_array); // To view your result
endforeach;
If you want Sysinfo as library..then do like this:
Your library should be name as Sysinfo.php:
class Sysinfo
{
var $info = array();
public function __construct()
{
$CI =& get_instance();
$settings = $CI->db->select("*")
->get("sysconfig")->result_array(); //Return result as array
foreach($settings as $setting) {
$this->info[$setting['sysopt']] = $setting['sysval'];
}
}
function get_sysinfo_data()
{
return $this->info;
}
}
In your Controller:
public function __construct()
{
$this->load->library('Sysinfo');
}
function index()
{
$data['arr_result'] = $this->Sysinfo->get_sysinfo_data();
$this->load->view('your_view',$data);
}
Hope it helps.
The error is appearing because you haven't extended your class with CI_Model base class.
Try defining your class as:
class Sysinfo extends CI_Model{ //your code }
will get rid of this error message.