Simple Slim application with Twig template - php

I'm trying to build simple Slim application with Twig template. The code is
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require '../vendor/autoload.php';
require_once '../vendor/twig/twig/lib/Twig/Autoloader.php';
Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem('../public');
$twig = new Twig_Environment($loader, array(
'cache' => '/cache',
));
$app = new \Slim\App;
$app->get('/', function (Request $request, Response $response) use ($twig) {
$template = $twig->loadTemplate('index.html');
echo $template->render(array());
//$response->getBody()->write("Test");
});
$app->run();
After running application I can see nothing in my browser.
But when I comment lines
$template = $twig->loadTemplate('index.html');
echo $template->render(array());
And remove comment from line
$response->getBody()->write("Test");
I can see Test in my browser which means that router '/' works.
File index.html exists and it's not emply
The content of index.html file is
<!DOCTYPE html>
<html>
<head>
<title>My Webpage</title>
</head>
<body>
<h1>My Webpage</h1>
</body>
</html>
How can I force template to be shown?

Related

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.

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

PHP require statement not working

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.

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:

Can't get Symfony 2 toolbar to display on website

I am trying to do this from a remote computer so the app_dev.php is:
<?php
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Debug\Debug;
$loader = require_once __DIR__.'/../app/bootstrap.php.cache';
Debug::enable();
require_once __DIR__.'/../app/AppKernel.php';
$kernel = new AppKernel('dev', true);
$kernel->loadClassCache();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
After the template renders I get this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Title</title>
</head>
<body>
<h1>Give us a toolbar now will you?</h1>
</body>
</html>
Looks like valid html and has a body element but no tool bar is there.
I have only ftp access so renamed the Symfony/app/cache folder to gone.
I have confirmed that app_dev.php executes Debug::enable(); and $kernel = new AppKernel('dev', true); (adding an error there causes an error.
I have confirmed that valid html is produced rendering index.html.twig (not a cached one). I have renamed the cache directory.
Not sure what else to check to have it show the tool bar.
You must enable the profiler in AppKernel.php
public function registerBundles()
{
$bundles = ...
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
...
}
return $bundles;
}
And make sure that you have JavaScript enabled.

Categories