简体   繁体   English

如何从 laravel 上的 php 文件调用 controller

[英]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? 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我共享目录结构和代码。我最后从 function OdemeBildir controller 目录结构尝试这个过程

php file structure php文件结构

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.由于您只调用 controller 文件,因此 Laravel 的核心和其他库将丢失。

1 - You may move your Bildirim_Islemleri file to App\Http\Controller folder as OdemeBildirController.php and define route on web.php 1 - 您可以将 Bildirim_Islemleri 文件作为 OdemeBildirController.php 移动到 App\Http\Controller 文件夹并在 web.ZE1BFD762321E409CEE4AC0B6E849 上定义路线

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

This will provide a route for your OdemeBildir function.这将为您的 OdemeBildir function 提供一条路线。

2 - If you don't want to use your file with Laravel, then you can communicate with Laravel thanks to queues. 2 - 如果您不想将文件与 Laravel 一起使用,那么您可以通过队列与 Laravel 进行通信。 https://laravel.com/docs/8.x/queues https://laravel.com/docs/8.x/queues

Queue provides you to do jobs in the background. Queue 让您可以在后台执行作业。 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.您可以使用将队列存储在 DB、Redis 或 Amazon SQS 中。 It's up to you but DB is enough in this situation.这取决于你,但在这种情况下 DB 就足够了。 Instead of calling MailController::siparisiniz_alindi_mail_gonder function, you will add a row in the jobs table.您将在作业表中添加一行,而不是调用 MailController::siparisiniz_alindi_mail_gonder function。 This will store information about your job.这将存储有关您的工作的信息。 Then you need to process it with a worker.然后,您需要与工作人员一起处理它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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