Exception Class request does not exists in index.php - 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.

Related

How to install Phalcon php

I downloaded Phalcon from official website
I copied php_phalcon.dll file to my xampp's php/ext directory
Edited the php.ini file located at D:\xampp\php\php.ini. and add there line extension=php_phalcon.dll at the end of the file.
I Restarted apache server and computer several times.
When I write phpinfo() to my code it seems phalcon was installed
Unfortunatelly Whan I try to run some code like
<?php
try {
// Autoloader
$loader = new \Phalcon\Loader();
$loader->registerDirs([
'../app/controllers/',
'../app/models/'
]);
$loader->register();
// Dependency Injection
$di = new \Phalcon\DI\FactoryDefault();
$di->set('view', function() {
$view = new \Phalcon\Mvc\View();
$view->setViewsDir('../app/views');
return $view;
});
// Deploy the App
$app = new \Phalcon\Mvc\Application($di);
echo $app->handle()->getContent();
} catch(\Phalcon\Exception $e) {
echo $e->getMessage();
}
?>
I get this error
Fatal error: Uncaught Error: Class 'Phalcon\Loader' not found in D:\xampp\htdocs\php-learning\public\index.php:4 Stack trace: #0 {main} thrown in D:\xampp\htdocs\php-learning\public\index.php on line 4
I also tried to follow the steps from tutorial on Phalcon ofical website where code looks somehow like this
<?php
use Phalcon\Di\FactoryDefault;
use Phalcon\Loader;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Application;
use Phalcon\Url;
// Define some absolute path constants to aid in locating resources
define('BASE_PATH', dirname(__DIR__));
define('APP_PATH', BASE_PATH . '/app');
// Register an autoloader
$loader = new Loader();
$loader->registerDirs(
[
APP_PATH . '/controllers/',
APP_PATH . '/models/',
]
);
$loader->register();
$container = new FactoryDefault();
$container->set(
'view',
function () {
$view = new View();
$view->setViewsDir(APP_PATH . '/views/');
return $view;
}
);
$container->set(
'url',
function () {
$url = new Url();
$url->setBaseUri('/');
return $url;
}
);
$application = new Application($container);
try {
// Handle the request
$response = $application->handle(
$_SERVER["REQUEST_URI"]
);
$response->send();
} catch (\Exception $e) {
echo 'Exception: ', $e->getMessage();
}
But didnĀ“t help. What I am doing wrong?
You have Phalcon installed successfully. However the namespace should be changed:
Moved Phalcon\Loader to Phalcon\Autoload\Loader #15797
please refer to the change log
enter link description here

Renamed index.php then Slim API stopped working

I had my slim API working, but then i decided to rename my index.php. After renaming it, it wouldn't work. So I renamed it back to index.php and deleted the other index.php. However, now it won't work at all.
My index looks as follows:
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require './vendor/autoload.php';
require './config/db.php';
//authenticator
// routes
require './routes/product.php';
require './routes/login.php';
require './routes/cart.php';
require './routes/wishlist.php';
// run app
$app->run();
My product route looks as follows:
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
$app = new \Slim\App;
// all entries
$app->get('/api/products/all', function (Request $request, Response $response) {
$sql = "SELECT * FROM product_endpoint";
try
{
$db = new DB();
$conn = $db->connect();
$stmt = $conn->query($sql);
$product = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
$response->getBody()->write(json_encode($product));
return $response
->withHeader('content-type', 'application/json')
->withStatus(200);
} catch (PDOException $e)
{
$error = ["message" => $e->getMessage()];
$response->getBody()->write(json_encode($error));
return $response
->withHeader('content-type', 'application/json')
->withStatus(500);
}
});
Before I renamed my index.php this worked perfectly, however now I'm just greeted with "page not found" when i navigate to my api endpoint.
index.php is located at root and the route is located in a file called "route".
Can anyone kindly assist? Thank you.

How to use multiple entry files to control routing in laravel

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

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.

Return http 500 with Slim framework

If somethings goes bad in my API i want to return a http 500 request.
$app = new Slim();
$app->halt(500);
It still return a http 200.
If i run this code:
$status = $app->response()->status();
echo $status; //Here it is 200
$status = $app->response()->status(500);
echo $status; //Here it is 500
it stills give me a http 200
The $app->response()->status(500); is correct, see the docs here.
Check to make sure you're calling $app->run(); after setting the status, this will prepare and output the response code, headers and body.
Edit, make sure you define a route or Slim will output the 404 response, this works:
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$app->response()->status(500);
$app->get('/', function () {
// index route
});
$app->run();
If anyone still has this issue here is what I ended up doing:
Setup an error handler
$app->error(function (Exception $exc) use ($app) {
// custom exception codes used for HTTP status
if ($exc->getCode() !== 0) {
$app->response->setStatus($exc->getCode());
}
$app->response->headers->set('Content-Type', 'application/json');
echo json_encode(["error" => $exc->getMessage()]);
});
then, anytime you need to return a particular HTTP status throw an Exception with the status code included:
throw new Exception("My custom exception with status code of my choice", 401);
(Found it on the Slim forum)
If you have to push header after $app->run(), you can always rely on the header php function:
header('HTTP/1.1 401 Anonymous not allowed');
Slim framework v2 wiki status
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$app->get('/', function () use ($app) {
$app->response()->setStatus(500);
$app->response()->setBody("responseText");
return $app->response();
});
$app->run();
or
$app->get('/', function () use ($app) {
$app->halt(500, "responseText");
});

Categories