PHP require statement not working - php

I have this code:
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require '../vendor/autoload.php';
require '../src/config/db.php';
$app = new \Slim\App;
$app->get('/hello/{name}', function (Request $request, Response $response) {
$name = $request->getAttribute('name');
$response->getBody()->write("Let's get started, $name");
return $response;
});
// Customer Routes
require '../src/routes/customers.php';
// Calendar Routes
require '../src/routes/calendar.php';
$app->run();
The require '../vendor/autoload.php'; is for Composer.
The browser only displays the last require statement, which is in this case:
// Calendar Routes
require '../src/routes/calendar.php';
If I want to display, for example, customers.php, then I would need to change the require order like this:
// Calendar Routes
require '../src/routes/calendar.php';
// Customer Routes
require '../src/routes/customers.php';
I would like to be able to display both require statemnts and $app->get('/hello/{name}' at once, with just typing different http://localhost in the browser.

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

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

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

Setting up the Slim framework

I'm following the tutorial on: http://www.slimframework.com/docs/tutorial/first-app.html
And it's telling me to add the following code to my index.php file:
<?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) {
$name = $request->getAttribute('name');
$response->getBody()->write("Hello, $name");
return $response;
});
$app->run();
However when I use Postman to do an API call to my server running the php program above I get the following response:

Categories