Using slim framework, put another website on a folder under public folder - slim

I am using Slim Framework, I need to put a wordpress under public folder.
slim-app/public/blog
The problema is when I do "www.myweb.com/blog" I obtain that "Page Not Found" is not found. By the momento I only have got this route in slim:
$app->get('/', function ($request, $response, $args) {
$this->logger->info("Slim-Skeleton '/' route");
return $this->renderer->render($response, 'index.phtml', $args);
});
Do I need to put any special route to do this?

Related

PHP dynamic URL keeps adding/doubling the full path when i click a button on the site

I am running into an issue and I hope someone here can help. I have put together a dynamic URL manipulator that should just work out the path to the file being displayed, which it does and when I click a link, it should process the new link and replace the existing URL. The issue is that instead of replacing the URL with the new url path, it adds it to the end of the existing URL which is why it does not work. an example is as follows:
https://localhost/home/main/localhost/home/main/pages/about
the part from the second localhost... should replace the first url path but it adds to it and therefore it does not work. the code for this is as follows:
class Core {
protected $currentController = 'Pages';
protected $currentMethod = 'index';
protected $params = [];
public function __construct(){
//print_r($this->getUrl());
$url = $this->getUrl();
if(file_exists('../app/controllers/' . ucwords($url[0]). '.php')){
$this->currentController = ucwords($url[0]);
unset($url[0]);
}
require_once '../app/controllers/'. $this->currentController . '.php';
$this->currentController = new $this->currentController;
if(isset($url[1])){
if(method_exists($this->currentController, $url[1])){
$this->currentMethod = $url[1];
unset($url[1]);
}
}
$this->params = $url ? array_values($url) : [];
call_user_func_array([$this->currentController, $this->currentMethod], $this->params);
}
public function getUrl(){
if(isset($_GET['url'])){
$url = rtrim($_GET['url'], '/');
$url = filter_var($url, FILTER_SANITIZE_URL);
$url = explode('/', $url);
return $url;
}
}
}
Hi anyone who cares to know the solution to this, or have a similar issue and is looking to find a solution.
I found out that the issue was with the htaccess file which did not have the full root of the home page.
I had entered
RewriteBase /main/public
my app is in a subfolder called home so after updating it to the following, it works like a charm.
RewriteBase /home/main/public

ZF2 application - Dispatch error event triggered every time

I'am working on a Zend Framework 2 application and have a strange behavior concerning error handling. My code in Module.php:
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$eventManager->attach(\Zend\Mvc\MvcEvent::EVENT_ROUTE, [$this, 'onPreRoute'], 100);
$eventManager->attach(\Zend\Mvc\MvcEvent::EVENT_DISPATCH_ERROR, [$this, 'handleError']);
}
public function onPreRoute(MvcEvent $e)
{
$serviceManager = $e->getTarget()->getServiceManager();
$router = $serviceManager->get('router');
$router->setTranslator($serviceManager->get('translator'));
}
public function handleError(MvcEvent $e)
{
$error = $e->getParam('error');
file_put_contents('error.log', $error . PHP_EOL, FILE_APPEND);
switch($error) {
case 'error-router-no-match':
$router = $e->getRouter();
$url = $router->assemble([], ['name' => 'home']);
header('Location: ' . $url);
exit;
}
}
As you can see I'am translating the routes. This works fine. But on every request the dispatch error event is triggered too. The error.log file will be created every time. But the redirect will be only performed if the route doesn't really exist. I think it depends on the translator or is my code in Module.php not correct?
Resolved!
The reason was that the browser automatically requests /favicon.ico and that was not available :-)

zend session namespace not working

I am unable to access zend session in included file via layout.
what i have done so far -
//bootstrap
public function _initSession()
{
Zend_Session::start();
$test = new Zend_Session_Namespace('test');
}
//controller
public function init(){
$test = new Zend_Session_Namespace('test');
$test->abc = 'defghi';
}
//layout include file
<?php include_once( APPLICATION_PATH . '/data/ga_test.php');?>
//ga_test.php
$test = new Zend_Session_Namespace('test');
echo 'this is ' . $test->abc;
I am not able to access the variable in ga_test file. I am getting an empty variable. But if I include ga_test end of each view file then it works. Obviously I don't want to go to every view file and include ga_test.php. Can I do this via layout.
I am sure, I am doing something wrong here. Any help would be really appreciated.
Thanks

Slim Framework - Using a GET route

I'm absolutely new to Slim Framework. I'm working on an Webservice that should provide an interface between an Android App and a Web-Application. I used the Slim Documentation to make my first steps and now I want to create a simple GET route, to receive information from the App. Here is what I have so far:
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$name_outside = '';
$app = new \Slim\Slim();
$app->get('/session/program_name/:name', function ($name) use($app) {
$name_outside = $name;
echo $name;
});
$app->run();
echo $name_outside;
I need to access the variable :name outside the function, but what I get is nothing. What I am doing wrong here?
Btw: I know that GET-routes usually are used to list existing resources, but for my simple case, I decided to use it that way.
Fix your code to hold name as args parameters ,then you can get in in Your function
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$name_outside = '';
$app = new \Slim\Slim();
$app->get('/session/program_name/{name}', function ($args) use($app) {
$name_outside = $args['name'];
echo $args['name'];
});
$app->run();
To acess the $name_outside inside the function context you pass it.
$app->get('/session/program_name/:name', function ($name) use($app, &$name_outside) {
$name_outside = $name;
echo $name;
});
But perhaps you're using Slim in a wrong way. Why you have to access the variable outside of your route?
No code is execyted after the run() call. That's the way slim works, try to put a die where you're echo the variable, it isn't reachable.
You shouldn't have the need to access the context of the route outside of it this way. To transform a request or a response you use middlewares with the hooks.

zend Framework. Calling a different javascript file specified by action/controller

I want to split a unique javascript file to get a file by action.
On my controllers I use jQuery()->addJavascriptFile() to call js files :
class DemandesController extends Zend_Controller_Action
{
public function init()
{
$this->view->jQuery()->addJavascriptFile('public/js/' . $this->getRequest()->getControllerName() . '/' . $this->getRequest()->getActionName() . '.js');
}
}
In my public/js folder I got a folder by controller and a js file by action...
It works well only with the indexAction, for all of the others the name of the controller is inserted into the URI :
With indexAction : http://192.168.78.208/demande_absence/public/js/demandes/index.js
With any other : http://192.168.78.208/demande_absence/demandes/public/js/demandes/nouvelle.js
What I've done wrong ?
Try:
$this->view->jQuery()->addJavascriptFile('/public/js/' . $this->getRequest()->getControllerName() . '/' . $this->getRequest()->getActionName() . '.js');
And i think you also have to use something like the baseurl view helper to build a correct url.
Maybe because it's not an absolute path. Try this:
$this->view->jQuery()->addJavascriptFile('/public/js/' . $this->getRequest()->getControllerName() . '/' . $this->getRequest()->getActionName() . '.js');