0

I want to run custom php code in laravel directly without using any routes or http requests..

I hope I can make it clear, I mean, like those online tools that runs php code by writing php code in browser, and then run it, and view result..

I found this handy project (Run-PHP-Code) to run PHP in browser directly, but I can't use models of my laravel project in PHP code..

How can I include laravel 's environment, so that I can for example:

$tag= new Tag;

where Tag is a model in laravel project, that would result into:

Fatal error: Class 'Tag' not found in D:\xampp\htdocs\widgetsRepository\app\controllers\Run-PHP-Code-master\index.php(49) : eval()'d code on line 3

Any idea? this would be very useful!

EDIT

I tried Brian suggestion at his answer, but I got this error now:

Call to a member function connection() on null

at vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php

public static function resolveConnection($connection = null)
{
    return static::$resolver->connection($connection);
}

so, I think I only need to get database sorted, then I can do experiments easily..

Samir Sabri
  • 937
  • 3
  • 11
  • 26
  • 3
    If you dont know what you are doing - this can be **extremely** dangerous. You should use a proven solution, and not try to make your own. It will ends in tears... – Laurence Apr 08 '15 at 07:31
  • Please check this link:-http://php-problems.blogspot.in/2013/07/adding-new-classes-or-library-to.html – Alive to die - Anant Apr 08 '15 at 07:31
  • @The Shift Exchange, is there a proven solution? I need something like CLI, to run PHP commands in terminal, but in IDE instead, what does the community use for run php code in laravel directly? – Samir Sabri Apr 08 '15 at 08:08
  • I suggest you download and study the script then build a Laravel package for it However, Like they said here "This script gives you the ability to quickly test snippets of PHP code locally" Its dangerous if you are using it on production. I don't see any reason why i should use it locally when there are lots of good IDEs out there – Emeka Mbah Apr 08 '15 at 08:19
  • hmm, so, do you think its better to just write a route to a testing controller, rather than testing outside laravel? – Samir Sabri Apr 08 '15 at 08:22
  • If you use a framework, and laravel is one, then you follow framework's rules. That means you create a testing controller and dump whatever code you want in there - it's that easy really. The other thing you're doing wrong is using some weird "test" suite from github and laravel itself comes with testing in mind already - why don't you add your code you want to test where it belongs - to tests? Don't complicate your life needlessly. – N.B. Apr 12 '15 at 09:10

1 Answers1

0

I've never tried to run code from a laravel project directly, I just copy and paste parts of the code into Run PHP Code.

That being said, it should be possible to run the code using the method taken from this StackOverflow question:


The Composer autoload script, to autoload all of your classes:

require __DIR__.'/../bootstrap/autoload.php';

And if you need things from the IoC container, you'll:

$app = require_once __DIR__.'/../bootstrap/start.php';

Then you will be able to do things like:

$post = Post::find(1);
Community
  • 1
  • 1
Brian Adams
  • 1,080
  • 8
  • 9