Ramzan Mahmood
Ramzan Mahmood

Reputation: 1901

Confused where to place php file (laravel 5.3)

I Have to Implement text message feature for my application for that i have a php file which i get from service provider where need to place db details.and give its path to service provider to add in server. I am confused where to place and according to him if i give that file path in url it should return contact number and message body but i am unable to test it.

script looks as

    <?php
    //  MySQL table outbox:
    // CREATE TABLE outbox(sender VARCHAR(255), rcpt VARCHAR(255), body VARCHAR(255));
    $mysql_host = "localhost";


    $mysql_base = "school_laravel";

    $mysql_user = "root";

    $mysql_password = "";

    $table = "outbox";

    mysql_connect($mysql_host, $mysql_user, $mysql_password);
    mysql_select_db($mysql_base);

    mysql_query("LOCK TABLES $table WRITE, $table AS $table" . "_read READ");

 $get_query = "SELECT * FROM $table AS $table" . "_read";
 $del_query = "DELETE FROM " . $table;
 if ($_GET['device'] != '') {
  $suffix = " WHERE sender='" . $_GET['device'] . "'";
     $get_query .= suffix;
  $del_query .= suffix;
 }
 $result = mysql_query($get_query);
    echo '<messages>';
    while ($array = mysql_fetch_array($result)) {
  echo '<message msisdn="' . $array['rcpt'] . '">' . $array['body'] . "</message>\n";
 }
 mysql_query($del_query);
 mysql_query("UNLOCK tables");
    echo '</messages>';
?>

My Question is that where should i place it in laravel Directory and is it possible to get a table details as i mentioned DB details in above script.

Thanks for Help

Upvotes: 0

Views: 68

Answers (1)

whoacowboy
whoacowboy

Reputation: 7447

Even if it saves you a minute, I would not use that code on your site. It would be difficult to use on a Laravel site unless you rewrite it. If you are going to have to rewrite it anyway, maybe you could do it in a Laravel way.

The script you have looks fairly straight forward to rewrite. This code is untested. I would not expect to copy and paste it into your app and have it work without modifications, but here are the basic steps.

Step 1 - Create a Model for your table.

app/Outbox.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Outbox extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'outbox';

}

Step 2 - Create a controller

app/Http/Controllers/MessagesController.php

<?php

namespace App\Http\Controllers;

use App\Outbox;
use Illuminate\Http\Request;

class MessagesController extends Controller
{
    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function show($sender)
    {

        $messages = Outbox::where('sender', $sender)
                      ->firstOrFail();

        $ids_to_delete = $messages->pluck('id');

        Outbox::destroy($ids_to_delete);

        return view('messages')
                 ->with('messages', $messages);;
    }
}

Step 3 - Create a view

resources/views/messages.blade.php

<!DOCTYPE html>
<html lang="en">
  <head>
      <title>
          laravel.com
      </title>
  </head>
  <body>
    <div>
      <messages>
        @foreach($messages as $message)
          <message msisdn="{{ $message->rcpt }}">{{ $message->body }}</message>
        @endforeach
    </div>
  </body>
</html>

Step 4 - Add a route for your new controller

app/routes/web.php

Route::get('messages/{sender}', 'MessagesController@show');

Step 5 - Visit the new URL

http://localhost/messages/device

Where device is whatever $_GET['device'] would have been.

Upvotes: 4

Related Questions