CodeIgniter Forums
I have to run some condition in every controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: I have to run some condition in every controller (/showthread.php?tid=79506)



I have to run some condition in every controller - mostafiz80 - 06-25-2021

I have to run some condition in every controller. i have posted my code below. now i use __construct 
how can i make it more nicely. now the full code i have to put in every controller but i want to make it other easier way.
Please help i am new in codeigniter but nowdays i loving it. i saw codeigniter before 10 years ago but i ignore it because thats time i was new in php.


PHP Code:
protected $request;
 public function 
__construct(){
 
helper(['url''form''device''cookie']);
 
parse_str($_COOKIE['siteAuth'], $output);
 
  $cookieHash $output['hash'];
 
  $usrs $output['usr'];
 
$usersModel = new \App\Models\UsersModel();
 
$userdata $usersModel->where('username'$usrs)->first();
 
$dbsid $userdata['sid'];
 
$username $userdata['username'];
 if(!
session()->has('pinverify')) {
 if(
get_cookie('siteAuth'true) != null AND ($usrs == $username) AND ($cookieHash == $dbsid)) {
 
header('Location: /auth/twosec');
 exit;
 } else {
 
header('Location: /auth/signout');
 exit;
 }
 }

    $devicedata device();
    if (session()->has('pinverify')){
 
$this->request = \Config\Services::request();
      $userid session()->get('user_id');
      $deviceModel = new \App\Models\DeviceModel();
      $isnew $deviceModel->where('uid'$userid)->countAllResults();
 
$agent $this->request->getUserAgent();
 
$browser $agent->getBrowser().' '.$agent->getVersion();
 
$osinfo $agent->getPlatform();
    $deviceinfo str_replace(' '''$agent);
 
  if ($isnew == 0) {
 
$devicedata = [
 
'uid' => $userid,
 
'deviceName' => $deviceinfo,
 
  'browser' => $browser,
 
  'osname' => $osinfo,
 
'lock_status' => '1',
 
'country' =>$devicedata['country'],
 
'state' =>$devicedata['countrystate'],
 
'isp' =>'ispdata',
 
'ip' => $this->request->getIPAddress(),
 ];
 if (
$deviceModel->insert($devicedata) == true){
 return 
true;
 }

 } else {
 
$device_info $deviceModel->find($userid);
 
  $devicedbname $device_info['deviceName'];
 
  $device_status $device_info['lock_status'];
 
  if ($device_status == 0){

 
$devicedata = [
 
'uid' => $userid,
 
'deviceName' => $deviceinfo,
 
  'browser' => $browser,
 
  'osname' => $osinfo,
 
'lock_status' => '1',
 
'country' =>$devicedata['country'],
 
'state' =>$devicedata['countrystate'],
 
'isp' =>'ispdata',
 
'ip' => $this->request->getIPAddress(),
 ];
 if(
$deviceModel->update($userid$devicedata) == true){
 return 
true;
 }
 }else{
 if (
$deviceinfo == $devicedbname){
 return 
true;
 }else{
 
header('Location: /auth/signout');
 exit;
 }
 }

 }

 }

 } 



RE: I have to run some condition in every controller - John_Betong - 06-25-2021

Hi @mostafiz80 and a warm welcome to the forums.

I have similar problems which I resolve by ensuring all Controllers extend BaseController:
PHP Code:
class C_home extends BaseController 

In BaseController use the following method:
file: ./app/Classes/BaseController
PHP Code:
public function initController
(
    
RequestInterface  $request
    
ResponseInterface $response
    
LoggerInterface   $logger
)
{
# Do Not Edit This Line
    
parent::initController($request$response$logger);

# John Added $data
    
$this->data = [
      
'test_001' => 'TEST to see if it works - 001',
      
'test_002' => 'TEST to see if it works - 002',
      
'test_003' => $this->common_function_with_data(),
];

}
//

private function common_function_with_data()
{
  return  
'COMMON DATA GOES HERE';
}
// 

To test try this in your common View/vheader menu or common View/vFooter:
PHP Code:
echo '<h6>' .$test_001 .'</h6>';
echo 
'<h6>' .$test_002 .'</h6>';
echo 
'<h6>' .$test_003 .'</h6>'



RE: I have to run some condition in every controller - paliz - 06-26-2021

initController.its better _constructor

I used tooo


RE: I have to run some condition in every controller - MGatner - 06-27-2021

I would recommend you look into Filters: https://codeigniter.com/user_guide/incoming/filters.html


RE: I have to run some condition in every controller - mostafiz80 - 06-28-2021

Thanks all of you guys for help. i have done using library. i like to use library. 
Now i am stuck in Mysql transaction. The mysql LOCK IN SHARE MODE how will work this query if i use codeigniter transaction. i have to do some transaction on my application.


RE: I have to run some condition in every controller - InsiteFX - 07-05-2021

Transaction lock and unlock the tables , what you have to watch for is dead locks.

To fix dead locks use small optimized queries.


RE: I have to run some condition in every controller - paliz - 07-05-2021

Use filter
, https://codeigniter.com/user_guide/incoming/filters.html?highlight=filter


RE: I have to run some condition in every controller - superior - 07-09-2021

Like @MGatner said, i would recommend using CodeIgniter 4 Filter as well.