-1

Just for experiment purpose. I have a self-made index.php and I have a Laravel model (made with php artisan make:model Something) named Something.php.

How can I load Something.php into index.php and have all functionalities like if the model loaded with Laravel?

Currently I have these codes:

<?php

# This is just for testing if Laravel model can be loaded independently

require_once('../vendor/autoload.php');

require_once('../bootstrap/app.php');

require_once('OtherPackage.php');
require_once('Testimonial.php');

use Test\Sample;
use App\Testimonial;

echo 'Hello World<br />';

$s = new Sample();
$t = Testimonial::all();

echo print_r($s, true);
echo '<br />';
echo print_r($t, true);

Use php -S localhost:8080 and it has this error:

[Wed Apr 25 15:02:50 2018] ::1:58542 [500]: / - Uncaught Error: Call to a member function connection() on null in /home/notalentgeek/notalentgeek/Temporary Working Folder Here/testing-testimonial-2-without-auths/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1135
Stack trace:
#0 /home/notalentgeek/notalentgeek/Temporary Working Folder Here/testing-testimonial-2-without-auths/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1101): Illuminate\Database\Eloquent\Model::resolveConnection(NULL)
#1 /home/notalentgeek/notalentgeek/Temporary Working Folder Here/testing-testimonial-2-without-auths/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(931): Illuminate\Database\Eloquent\Model->getConnection()
#2 /home/notalentgeek/notalentgeek/Temporary Working Folder Here/testing-testimonial-2-without-auths/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(877): Illuminate\Database\Eloquent\Model->newBaseQueryBuilder()
#3 /home/notalentgeek/notalentgeek/Temporary Working Folder Here/testing-testi in /home/notalentgeek/notalentgeek/Temporary Working Folder Here/testing-testimonial-2-without-auths/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 1135

I have tried to use $app->withEloquent() as it is told in PHP Lumen Call to a member function connection() on null. But, it returns new error:

[Wed Apr 25 15:10:08 2018] ::1:58700 [500]: / - Uncaught Error: Call to undefined method Illuminate\Foundation\Application::withEloquent() in /home/notalentgeek/notalentgeek/Temporary Working Folder Here/testing-testimonial-2-without-auths/app/index.php:10

My goal is that I want to refactor old codes with Laravel. So, I want to know what is the best way to "launch" Laravel with my old codes cover ON TOP of the Laravel project.

notalentgeek
  • 4,939
  • 11
  • 34
  • 53

1 Answers1

0

Laravel is a php framework.

It was designed with a specific way of working in mind. This way of working is extensively documented throughout the web. Even though it might be necessary to overwrite Classes and hack the framework from time to time, generally the first approach to building something using Laravel is to use it how it is meant to be used.

Which means you shouldn't be messing around in the index.php file. This is a file which loads the rest of the framework.

Start working with Routes, Models, Controllers and views. If you want to integrate your old code into a fresh Laravel application, try to figure out how your code can fit into the MVC structure, any other code you should organise in services, which you can then inject into your controllers.

For more information take a look at the following links.

https://nl.wikipedia.org/wiki/Model-view-controller-model

https://laravel.com/docs/5.6/routing

Mkk
  • 433
  • 3
  • 8