简体   繁体   中英

How to call controller from .php file on laravel

I have name is Bildirim_Islemleri.php php file on laravel public directory.I need call Controller in this php file.And return cath error to insert log table.How can ı do this?Do you have any ideas?

Im sharing directory stcuture and code.I trying this process in last from function OdemeBildir controller directory structure

php file structure

enter image description here

<?php
/**
 *
 */

class Bildirim_Islemleri
{
  // use App\Http\Controllers\MailController;
  function __construct() {
    $yol = "../config/DBConfig.php";
    require_once( $yol );


  }
  public function OdemeBildir($post)
  {
    $dbConfig=new DBConfig();
    $baglanti = new PDO("mysql:host=".$dbConfig->host.";dbname=".$dbConfig->dbname, $dbConfig->dbuser, $dbConfig->password);
    $baglanti->exec("SET NAMES utf8");
    $baglanti->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $siparis_kodu=$post["merchant_oid"];
    $sepetGetirSql = "select * from sepetler where siparis_kodu='".$siparis_kodu."'";
    //
    $sepetler = array();


  $queryResultSepetList = $baglanti->query( $sepetGetirSql, PDO::FETCH_ASSOC );
  if ( $queryResultSepetList->rowCount() > 0 ) {
      foreach ( $queryResultSepetList as $row ) {
          array_push( $sepetler, $row );
        }
      }


$uye_id="";
for ($i=0; $i <count($sepetler) ; $i++) {
  $statement = $baglanti->prepare("SELECT urun_adi,urun_fiyati FROM urunler WHERE urun_id = :urun_id");
  $statement->execute(array( ":urun_id" => $sepetler[$i]["urun_id"] ));
  $urun_bilgileri = $statement->fetch(PDO::FETCH_ASSOC);


  $prepare = $baglanti->prepare("INSERT INTO siparisler SET urun_id = :urun_id,miktar=:miktar,urun_fiyati=:urun_fiyati,siparis_kodu=:siparis_kodu,kargo_takip_no=:kargo_takip_no,odeme_durumu=:odeme_durumu,uye_id=:uye_id");

  $veriler = [
      "urun_id" =>  $sepetler[$i]["urun_id"],
      "miktar" =>  $sepetler[$i]["miktar"],
      "urun_fiyati" =>  $urun_bilgileri["urun_fiyati"],
      "siparis_kodu" =>  $sepetler[$i]["siparis_kodu"],
      "kargo_takip_no" =>  "",
      "odeme_durumu" => 1,
      "uye_id" => $sepetler[$i]["uye_id"]
  ];
  $uye_id=$sepetler[$i]["uye_id"];
  // verileri çalıştır
  $prepare->execute($veriler);


    }
    // $sepet_sil=$baglanti->prepare("DELETE FROM sepetler WHERE siparis_kodu=:siparis_kodu");
    // $sepet_sil->bindParam(":siparis_kodu",$siparis_kodu,PDO::PARAM_STR);
    // $sepet_sil->execute();
    try {
    App\Http\Controllers\MailController::siparisiniz_alindi_mail_gonder("emrahcaliskan33@outlook.com", "Emrah Çalışkan",$siparis_kodu);
    } catch (Error  $e) {
      $this->HataLogKaydet($uye_id,$siparis_kodu,$e->getMessage());
    }



  }
  public function HataLogKaydet($uye_id,$siparis_kodu,$hata_log)
  {

    $dbConfig=new DBConfig();
    $baglanti = new PDO("mysql:host=".$dbConfig->host.";dbname=".$dbConfig->dbname, $dbConfig->dbuser, $dbConfig->password);
    $baglanti->exec("SET NAMES utf8");
    $baglanti->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $prepare = $baglanti->prepare("INSERT INTO hata_loglari SET uye_id = :uye_id,siparis_kodu=:siparis_kodu,hata_log=:hata_log");

    $veriler = [
        "uye_id" =>  $uye_id,
        "siparis_kodu" =>  $siparis_kodu,
        "hata_log" =>  $hata_log
    ];
    // verileri çalıştır
    $prepare->execute($veriler);
  }
}

 ?>

Since you only call the controller file, Laravel's core and other Libraries will be missing.

1 - You may move your Bildirim_Islemleri file to App\Http\Controller folder as OdemeBildirController.php and define route on web.php

Route::post('odeme/odeme-bildir', [App\Http\Controllers\OdemeBildirController::class, 'OdemeBildir']);

This will provide a route for your OdemeBildir function.

2 - If you don't want to use your file with Laravel, then you can communicate with Laravel thanks to queues. https://laravel.com/docs/8.x/queues

Queue provides you to do jobs in the background. You create jobs and a worker processes them in the background by order. You can use store your queue in DB, Redis, or Amazon SQS. It's up to you but DB is enough in this situation. Instead of calling MailController::siparisiniz_alindi_mail_gonder function, you will add a row in the jobs table. This will store information about your job. Then you need to process it with a worker.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM