I worked on a symfony 3 project that an ex-colleague set up.
I'm beginner with Symfony.
I don't know how to autoload data (parameters) from database to set in php constants
The app begin in this file : /web/app_matnat_dev.php
<?php
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Debug\Debug;
require_once 'access_control.inc.php';
ini_set("error_reporting", E_ALL);
ini_set("display_errors", "1");
/**
* #var Composer\Autoload\ClassLoader $loader
*/
$loader = require __DIR__.'/../app/autoload.php';
Debug::enable();
$kernel = new AppKernel($_SERVER['SFENV'], true);
$kernel->loadClassCache();
$request = Request::createFromGlobals();
try {
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
} catch (Exception $e) {
var_dump($e);
}
It includes /app/autoload.php :
<?php
use Doctrine\Common\Annotations\AnnotationRegistry;
use Composer\Autoload\ClassLoader;
/**
* #var ClassLoader $loader
*/
$loader = require __DIR__.'/../vendor/autoload.php';
AnnotationRegistry::registerLoader([$loader, 'loadClass']);
/* Ensemble de fonctions dont j'ai besoin */
require_once __DIR__.'/../src/RecupBundle/env.php';
require_once __DIR__.'/../src/RecupBundle/autoload_orm.php';
//require_once __DIR__.'/../src/RecupBundle/constants.php';
require_once __DIR__.'/../src/RecupBundle/functions.php';
return $loader;
That's here i'd like (i think i can) to load from database my parameters and after include others parameters
require_once __DIR__.'/../src/RecupBundle/constants.php';
i call a method in first lines in this file
// database parameters to constants
use function RecupBundle\ORM\param;
RecupBundle\ORM\param()->load();
But i've this error
Notice: Undefined index: db_link in /src/RecupBundle/ORM/db_query.php on line 44
Because db_link is only defined in /web/app_matnat_dev.php when i do that $response = $kernel->handle($request);
I need help :)
Thanks
I've found a trick: I do request just after the connexion to the database.
So, I can define the constants used everywhere in the project.
Related
I'm just getting started with PHP and I ran into a small problem.
I've downloaded a package using composer require <package_name> and now I'm not sure how to access it from my .php file. I tried bunch of things but I couldn't make it work.
I'm trying to use this package: https://packagist.org/packages/giggsey/libphonenumber-for-php
EDIT:
This is the code I use to test:
<?php
use Slim\Factory\AppFactory;
require __DIR__ . '/../vendor/autoload.php';
// Instantiate app
$app = AppFactory::create();
// Add Error Handling Middleware
$app->addErrorMiddleware(true, false, false);
// Register routes
$routes = require __DIR__ . '/../app/routes.php';
$routes($app);
$swissNumberStr = "044 668 18 00";
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
try {
$swissNumberProto = $phoneUtil->parse($swissNumberStr, "CH");
var_dump($swissNumberProto);
} catch (\libphonenumber\NumberParseException $e) {
var_dump($e);
}
// Run application
$app->run();
And it says: Undefined type 'libphonenumber\PhoneNumberUtil'.
At present, I have the following routes
Route::post('/report', [App\Http\Controllers\ServerController::class, 'report'])->name('report');
Route::get('/report', [App\Http\Controllers\ServerController::class, 'getData'])->name('get_data');
Route::get('/other',[App\Http\Controllers\ServerController::class, 'other'])->name('other');
Route::get('/other2',[App\Http\Controllers\ServerController::class, 'other2'])->name('other2');
Route::get('/other3',[App\Http\Controllers\ServerController::class, 'other3'])->name('other3');
...
I create report.php in the public directory
I want to bind the get and post requests of report.php to report and get_data
But I didn't find a similar explanation in the official document
What should I do?
Update:2021-05-21
I found a solution, but it didn't look elegant
Are there any other solutions?
<?php
// this is public/report.php
use Illuminate\Http\Request;
use Illuminate\Contracts\Http\Kernel;
define('LARAVEL_START', microtime(true));
if (file_exists(__DIR__ . '/../storage/framework/maintenance.php')) {
require __DIR__ . '/../storage/framework/maintenance.php';
}
require __DIR__ . '/../vendor/autoload.php';
$app = require_once __DIR__ . '/../bootstrap/app.php';
$_SERVER['REQUEST_URI'] = "/report.php/report";
$_SERVER['DOCUMENT_URI'] = "/report.php/report";
// var_dump($_SERVER);
$kernel = $app->make(Kernel::class);
$response = tap($kernel->handle(
$request = Request::capture()
))->send();
$kernel->terminate($request, $response);
i have uploaded my laravel project to live server but its not working when i track code i get the execption that class request does not exists,please help me to find out the error
this is my file
index.php
<?php
require __DIR__.'/bootstrap/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
$app->bind('path.public', function() {
return __DIR__;
});
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
try{
$request = App\Http\Request::capture()
}
catch(Exception $e)
{
echo "error:".$e->getMessage()."<br>";
}
$response = $kernel->handle(
$request = App\Http\Request::capture()
);
$response->send();
$kernel->terminate($request, $response);
Request facade usually lives in a different namespace. Try replacing any reference to
App\Http\Request
with just
Request
in order to access the facade by its global namespace alias.
For debugging purposes I want to log all headers from the response object like so in filters.php:
App::after(function($request, $response) {
if(App::environment() !== 'dev') { return; }
error_log(print_r($response->getHeadersPleaaase()));
$response is typically a Illuminate\Http\JsonResponse and I cannot find any obvious method for getting the headers there.
Headers is a public property:
error_log(print_r($response->headers));
ini_set("log_errors", 1);
ini_set("error_log", __DIR__.'/../app/logs/prod.log');//ini_set("error_log", your path and file);
error_log( 'BEGIN===========>' );
error_log( $request );
error_log( $response );
error_log( '<========END' );
an example of using (Symfony2)
app.php
<?php
use Symfony\Component\ClassLoader\ApcClassLoader;
use Symfony\Component\HttpFoundation\Request;
$loader = require_once __DIR__.'/../app/bootstrap.php.cache';
// Use APC for autoloading to improve performance.
// Change 'sf2' to a unique prefix in order to prevent cache key conflicts
// with other applications also using APC.
/*
$apcLoader = new ApcClassLoader('sf2', $loader);
$loader->unregister();
$apcLoader->register(true);
*/
require_once __DIR__.'/../app/AppKernel.php';
//require_once __DIR__.'/../app/AppCache.php';
$kernel = new AppKernel('prod', false);
$kernel->loadClassCache();
//$kernel = new AppCache($kernel);
// When using the HttpCache, you need to call the method in your front controller instead of relying on the configuration parameter
//Request::enableHttpMethodParameterOverride();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
ini_set("log_errors", 1);
ini_set("error_log", __DIR__.'/../app/logs/prod.log');
error_log( 'BEGIN===========>' );
error_log( $request );
error_log( $response );
error_log( '<========END' );
$response->send();
$kernel->terminate($request, $response);
I would like to preapare the app.php file with Symfony 2.1 and APC. I took the Symfony standard edition configured it with doctrine and then made changes described here:
http://symfony.com/doc/2.1/book/performance.html
<?php
require_once __DIR__.'\..\vendor\symfony\symfony\src\Symfony\Component\ClassLoader\UniversalClassLoader.php';
require_once __DIR__.'\..\vendor\symfony\symfony\src\Symfony\Component\ClassLoader\ApcUniversalClassLoader.php';
use Symfony\Component\ClassLoader\ApcClassLoader;
use Symfony\Component\HttpFoundation\Request;
$loader = new ApcClassLoader('sf2', $loader);
$loader->register(true);
require_once __DIR__.'/../app/AppKernel.php';
$kernel = new AppKernel('prod', false);
$kernel->loadClassCache();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
?>
but it is not working, I got error:
Fatal error: Class 'Symfony\Component\HttpKernel\Kernel' not found in __my_path__\app\AppKernel.php on line 7
How should I prepare the app.php for best performance.
Take a look at the default app.php file in the Symfony Standard Distribution. In order to enable APC your app.php file should look like this:
use Symfony\Component\ClassLoader\ApcClassLoader;
use Symfony\Component\HttpFoundation\Request;
$loader = require_once __DIR__.'/../app/bootstrap.php.cache';
// Use APC for autoloading to improve performance.
// Change 'sf2' to a unique prefix in order to prevent cache key conflicts
// with other applications also using APC.
$loader = new ApcClassLoader('sf2', $loader);
$loader->register(true);
require_once __DIR__.'/../app/AppKernel.php';
//require_once __DIR__.'/../app/AppCache.php';
$kernel = new AppKernel('prod', false);
$kernel->loadClassCache();
//$kernel = new AppCache($kernel);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
You can also improve performance if you enable the symfony gateway cache by uncommenting the require_once __DIR__.'/../app/AppCache.php'; and $kernel = new AppCache($kernel); lines.