How to use multiple entry files to control routing in laravel - php

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);

Related

How to import/use package installed with Composer?

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'.

Symfony 3 Autoload data from database

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.

index.php page not working with slim framwork api in share hosting

I have created a slim project in godaddy. after creation, I verify everything works by going to pctechtips.org/apps/slimpro/public and I get the slim webpage. the default index.php inside public directory contaings some code to display that page. when I remove the code and replace it with mine. It doesn't display anything. what is wrong?
default slim install index.php
<?php
if (PHP_SAPI == 'cli-server') {
// To help the built-in PHP dev server, check if the request was actually for
// something which should probably be served as a static file
$url = parse_url($_SERVER['REQUEST_URI']);
$file = __DIR__ . $url['path'];
if (is_file($file)) {
return false;
}
}
require __DIR__ . '/../vendor/autoload.php';
session_start();
// Instantiate the app
$settings = require __DIR__ . '/../src/settings.php';
$app = new \Slim\App($settings);
// Set up dependencies
require __DIR__ . '/../src/dependencies.php';
// Register middleware
require __DIR__ . '/../src/middleware.php';
// Register routes
require __DIR__ . '/../src/routes.php';
// Run app
$app->run();
my index.php page
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require 'vendor/autoload.php';
$app = new \Slim\App;
$app->get('/hello/{name}', function (Request $request, Response $response, array $args) {
$name = $args['name'];
$response->getBody()->write("Hello, $name");
return $response;
});
$app->run();
?>
I Think the problem here is about the path of composer auto load file
Its not correct to be in the public directory
In default index.php
require __DIR__ . '/../vendor/autoload.php';
In your code
require 'vendor/autoload.php';

Google Cloud Storage 500 Internal Server Error

SettingsController.php:
namespace App\Controllers;
use Google\Cloud\Storage\StorageClient;
class SettingsController extends Controller {
public function createBucket($request, $response) {
$projectId = 'myProjectId'; //here I specified my projectId
$storage = new StorageClient([
'projectId' => $projectId
]);
$bucketName = 'socnetfilestestkekdsfa213kfh34';
$bucket = $storage->createBucket($bucketName);
}
}
So, I wrote this code in accordance with documentation, but I'm always getting 500 Network Error.
This is my index.php:
<?php
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../config/db.php';
$app = new \Slim\App(['settings' => $config]);
require __DIR__ . '/../app/dependencies.php';
require __DIR__ . '/../app/routes.php';
$app->run();
And this is my project structure:
Finally, I've solved the problem.
As I mentioned require __DIR__ . '/../vendor/autoload.php'; was correct. In my case, the reason of 500 Error was an environment variable. For the deployed app you should put your Service Google Cloud Account key config.json into your project. I put mine in a config folder (check my project structure above).
It means that in Heroku 'Settings' i need to specify
GOOGLE_APPLICATION_CREDENTIALS: ../config/yourAppName.json
Thanks this article:
https://medium.com/#naz_islam/how-to-authenticate-google-cloud-services-on-heroku-for-node-js-app-dda9f4eda798

Exception Class request does not exists in index.php

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.

Categories