How to get the root URL to Slim application in a subdir? - .htaccess

I must make a website with PHP and I choose to use Slim and Twig. But my superiors don't want me to use a virtual host. So I'm having trouble when I test the website with MAMP because the site is on a subdirectory, like http://localhost:8888/subdir.
When I try to access an asset, I can't use absolute path because it would force me to write /subpath/path/to/asset. But when we will deploy the application, there will be no subpath. How can I root the website as if there would be a virtual host?
You can see some of my code below:
index.php
<?php
require 'vendor/autoload.php';
include 'database.php';
use app\controller\ConfigController;
$app = new \Slim\Slim();
$app->get('/', function () {
echo "accueil";
})->name("root");
$app->group('/Admin', function () use ($app) {
$app->get("/", function (){
$ctrl = new ConfigController();
$ctrl->index();
})->name("indexAdmin");
});
.htaccess (in localhost:8888/subdir)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
ConfigController (called function)
public function index() {
$loader = new \Twig_Loader_Filesystem("app/view/Admin");
$twig = new \Twig_Environment($loader);
$template = $twig->loadTemplate('Index.twig');
echo $template->render(array(
'css' => "admin.css"
));
}
template called by Twig environment
<!doctype html>
<html lang="fr">
<head>
<link rel="stylesheet" type="text/css" href="/app/assets/stylesheets/{{ css }}">
</head>
<body>
[...]
When I search on Google and Stack Overflow, everyone said to make a virtual host, but I can't. Would there be another solution?

If you are using the slim/views package to integrate Twig in the application you are writing, it's possible to add the TwigExtension in that package to your Twig instance and use the siteUrl function for your assets. This way:
<link rel="stylesheet" href="{{ siteUrl('path/to/asset/style.css') }}">
If you're not using that package, you can create your own function to get the application URL. Something like this:
function siteUrl($url) {
$req = Slim::getInstance()->request();
$uri = $req->getUrl() . $req->getRootUri();
return $uri . '/' . ltrim($url, '/');
}

Related

How to render template from text?

I want to store in my database a large text from an HTML file that could have Twig directives such as double curly braces, pipes, etc. From that string, I want to render a Twig template and send it back to the client.
public function renderAction (Request $request) {
$text = "<!doctype html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<title>Document</title>
</head>
<body>
{{SomeVariable}}
</body>
</html>";
$template = $this->render($text, $parameters);
return new JsonResponse(["status" => "ok", "template" => $template], 200);
}
When I try this I got an error, it seems like the first param from $this->render is always used as a path to a twig file but I will not be storing URI but the HTML file content in my database. Is there any way to render a twig template from a string?
I also tried using
$template = $this->get('twig')->createTemplate($text);
return new JsonResponse(["status" => "ok", "template" => $template], 200);
but the "template" prop is always empty when the client receives it
I am using Symfony 2.8.52 and PHP 5.6

hbs rendered site with handlebars.js in the script - nodejs

I use hbs to render my pages with partials for navigation and footers.
router.get('/test', function (req, res) {
return res.render('test');
});
On one page I have template that uses mustache.js. This template doesn't work as it should as the {{}} seems to be picked up on the hbs render. Below is a basic example that illustrates the error. If I load this as a static page with express I get "Joe is a Web Developer", if I render it with hbs I get "is a".
Are there any work arounds that wont involve me changing how all my pages are rendered?
<!doctype html>
<html lang="en">
<head>
<title>Mustache.js Inline Method</title>
<script type="text/javascript" src="js/libs/mustache.js" ></script>
<script>
var view = {
name : "Joe",
occupation : "Web Developer"
};
function loadtemp(){
var output = Mustache.render("{{name}} is a {{occupation}}", view);
document.getElementById('person').innerHTML = output;
}
</script>
</head>
<body onload="loadtemp()" >
<p id="person"></p>
</body>
</html>
It was simple enough. I just had to escape the brackets with a \
So all it took was
\{{name}}

Twig show source code but not HTML

I'm trying to run Twig (standalone, without Symfony 2) in a PHP project (Wamp) but it doesn't work : Twig shows the templates source code but does not execute the HTML. I don't find why. Here is my code :
index.php:
// DOCUMENT_ROOT (constant defined in a previous included file) : C:/Program Files/wamp/www/project
$sTwigPath = DOCUMENT_ROOT."/vendors/twig/lib/Twig/Autoloader.php";
if (file_exists($sTwigPath)) {
require($sTwigPath);
Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem(DOCUMENT_ROOT."/templates");
$tpl = new Twig_Environment($loader, array("cache"=>false));
try {
$oTemplate = $tpl->loadTemplate("home.html");
} catch (Twig_Error_Loader $e) {
error_log("Twig library not found", 0);
exit;
}
$aRender = array();
$aRender["myVar"] = "Wouah! myVar is displayed here!";
echo $oTemplate->render($aRender);
home.html :
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
<h1>Home</h1>
{{myVar}}
</body>
</html>
I've no error (checked in PHP and apache logs).

Node.js and swig template engine - including template inside template

I trying to create main page (part of node.js and mongoDB application) that includes login form.
To add view part I included js files with function that returns HTML, but as I can see much better is using template engine.
Everything is OK until I including one compiled swig part inside another one.
The output of main page is OK, but login part outputs like text on the page.
How is possible to output the login HTML as HTML instead of plain text?
Does more information needed to understand the issue?
Thank you in advance.
var swig = require('swig');
var mainPage_tpl = swig.compileFile(__dirname+'/../views/mainpage_tpl.html');
var formLogin_tpl = swig.compileFile(__dirname+'/../views/login_tpl.html');
var loginOutput = formLogin_tpl();
var mainPageOutput = mainPage_tpl({
title: 'Sometitle',
pagetitle: 'Somepagetitle',
content: loginOutput
});
exports.get = function(req, res){
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(mainPageOutput);
res.end();
}
mainpage_tpl.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>{{title}}</title>
<link rel="stylesheet" href="/assets/reset.css" />
<link rel="stylesheet" href="/assets/style.css" />
<script type="text/javascript" src="/assets/jquery-2.0.0.js"></script>
<script type="text/javascript" src="/assets/script.js"></script>
</head>
<body id="login_page">
<h1>{{pagetitle}}</h1>
<div id="content">{{content}}</div>
</body>
</html>
If you want to include literal HTML, you need to tell Swig to not escape it using the safe filter:
...
<div id="content">{{ content|safe }}</div>
...

Codeigniter page shows 404 page not found

I am new to codeIgniter and Linux as well. When I tried to run http://localhost/CodeIgniter/hello/first_page it shows 404 Error: Page not found. My Controller class :
/*hello.php*/
class Hello extends CI_Controller
{
function __construct() {
parent::__construct();
}
function first_page()
{
$this->load->view('first_view');
}
}
And my first_view.php is :
<html>
<head>
<title>First CI Page</title>
</head>
<body>
Hi Folks !
</body>
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
?>
</html>
NB: http://localhost/CodeIgniter shows the welcome message.And all files are executable(777)
Try this url.. You'll get it
http://localhost/CodeIgniter/index.php/hello/first_page
You have to include index.php

Resources