Welcome Guest, Not a member yet? Register   Sign In
I have to run some condition in every controller
#1

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;
 }
 }

 }

 }

 } 
Reply
#2

(This post was last modified: 07-04-2021, 10:43 PM by John_Betong.)

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>'
Reply
#3

initController.its better _constructor

I used tooo
Enlightenment  Is  Freedom
Reply
#4

I would recommend you look into Filters: https://codeigniter.com/user_guide/incom...lters.html
Reply
#5

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.
Reply
#6

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

To fix dead locks use small optimized queries.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

Use filter
, https://codeigniter.com/user_guide/incom...ght=filter
Enlightenment  Is  Freedom
Reply
#8

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




Theme © iAndrew 2016 - Forum software by © MyBB