PHP Slim4 on Apache2 gives HttpNotFoundException - php

Links I've seen include:
https://akrabat.com/running-slim-4-in-a-subdirectory/
https://discourse.slimframework.com/t/slim-4-httpnotfoundexception/3273/18
and some others
I'm trying to setup a Slim4 project on apache2. But trying to access it gives me the error
PHP Fatal error: Uncaught Slim\Exception\HttpNotFoundException: Not found....
I have my index file in /var/www/html/_projects/work/calc1/src.
The following is my calc.conf in /etc/apache2/sites-available:
<VirtualHost *:80>
ServerName calc.mrid.local
ServerAlias calc.mrid.local
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/_projects/work/calc1/src
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
This is my .htaccess located in src directory
RewriteEngine On
RewriteBase /_projects/work/calc1/src/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
My index.php looks like this:
// use and require statements...
$container = new Container();
AppFactory::setContainer($container);
$container->set('view', function() {
return \Slim\Views\Twig::create(__DIR__ . '/public/templates');
});
$app = AppFactory::create();
// even without this line it doesn't work
$app->setBasePath("/_projects/work/calc1/src");
$app->add(TwigMiddleware::createFromContainer($app));
// my routes....
$app->run();
Has anyone worked with Slim4 on Apache2? If yes, can you please guide me ?

The src/ directory is actually only meant for the PHP classes and must not be accessible to the web. The apache DocumentRoot should point to the public/ directory which contains your front controller public/index.php. Take a look at the Standard PHP package skeleton. More details about Slim and apache can be found in this Slim 4 Tutorial.

Related

Laravel 8.53 - Routing doesnt work on any of pages

Solution
The solution was found thanks to #Gert B.
Simply add to your Laravel application any virtual host (I added for mine)
laravel1.test
How to add Virtual host:
Go to C:\Windows\System32\drivers\etc\hosts
add line:
127.0.0.1 laravel1.test (or your virtual host name)
And add this to your vhosts(in case of using xampp) in C:\xampp\apache\conf\extra httpd-vhosts
<VirtualHost *:80>
ServerName www.laravel1.test
ServerAlias laravel1.test
DocumentRoot "C:\xampp\htdocs\Laravel1\public"
<Directory "C:\xampp\htdocs\Laravel1\public">
# use mod_rewrite for pretty URL support
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
# use index.php as index file
DirectoryIndex index.php
# ...other settings...
# Apache 2.4
Require all granted
## Apache 2.2
# Order allow,deny
# Allow from all
</Directory>
</VirtualHost>
I can't pass the problem with routing in Laravel 8.5 framework. I know there have been many requests about this problem so far but any of given solutions didnt help in my case. I will show Frontend code, since backend is the same thing and also doesnt work. Right now, the only thing that works is index.php.
I know thanks to newest Laravel all bugs are hidden under simple error 404.
Bug image
I already tried:
Changing in apache/conf/httpd solution AllowOverride All
Clearing whole cache and routes cache
Any possible misspells in code
Still, without any working anwser.
My FrontendController at app/Http/Controllers
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class FrontendController extends Controller
{
public function index()
{
return view('frontend.index');
}
public function object()
{
return view('frontend.object');
}
public function article()
{
return view('frontend.article');
}
public function person()
{
return view('frontend.person');
}
public function room()
{
return view('frontend.room');
}
public function roomSearch()
{
return view('frontend.roomsearch');
}
}
My web.php at app/routes
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\FrontendController;
##Frontend routes
Route::get('/','FrontendController#index')->name('index');
Route::get('/object','FrontendController#object')->name('object');
Route::get('/article','FrontendController#article')->name('article');
Route::get('/person','FrontendController#person')->name('person');
Route::get('/room','FrontendController#room')->name('room');
Route::get('/roomSearch','FrontendController#roomsearch')->name('roomSearch');
##Backend routes
Route::group(['prefix'=>'admin'],function(){
Route::get('/','BackendController#index')->name('adminHome');
Route::get('/cities','BackendController#cities')->name('cities');
Route::get('/myObjects','BackendController#myobjects')->name('myObjects');
Route::get('/profile','BackendController#profile')->name('profile');
Route::get('/saveObject','BackendController#saveobject')->name('saveObject');
Route::get('/saveRoom','BackendController#saveroom')->name('saveRoom');
});
.htacces file in app/public
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
Options +FollowSymlinks
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
My routes
image
Solution
The solution was found thanks to #Gert B.
Simply add to your Laravel application any virtual host (I added for mine) laravel1.test
How to add Virtual host: Go to C:\Windows\System32\drivers\etc\hosts
add line:
127.0.0.1 laravel1.test (or your virtual host name)
And add this to your vhosts(in case of using xampp) in C:\xampp\apache\conf\extra httpd-vhosts
<VirtualHost *:80>
ServerName www.laravel1.test
ServerAlias laravel1.test
DocumentRoot "C:\xampp\htdocs\Laravel1\public"
<Directory "C:\xampp\htdocs\Laravel1\public">
# use mod_rewrite for pretty URL support
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
# use index.php as index file
DirectoryIndex index.php
# ...other settings...
# Apache 2.4
Require all granted
## Apache 2.2
# Order allow,deny
# Allow from all
</Directory>
</VirtualHost>

PHP and Redirect with apache2.conf

I have an internal domain like
live.domain.com
which I defined in sites-available like so
ServerName live.domain.com
ServerAdmin webmaster#localhost
DocumentRoot /data/www/html/mydomainnamenodots
<Directory /data/www/html/mydomainnamenodots>
Require all granted
</Directory>
and the accordingly same for SSL conf.
I do have a folder /api which is in there like
/data/www/html/mydomainnamenodots/api
What I want is to catch all calls to
live.domain.com/api/*
and redirect them to something like
live.domain.com/api/index.php?url=*
So a real example would be
live.domain.com/api/statistics/getFTE/123/456
and I'd like that to be
live.domain.com/api/index.php?url=/statistics/getFTE/123/456
That would result in a $_REQUEST in index.php like so
array(1) {["url"]=>string(33) "/statistics/getFTE/123/456"}
mod_rewrite is enabled and loaded. Simple tests with file to file redirects work.
My rewrite rule which does not work as it does not redirect is
<Directory "/data/www/html/mydomainnamenodots/api">
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*?mydomainnamenodots\/api(.*)$ /mydomainnamenodots/api/index.php?url=$1 [QSA,L]
</Directory>
Any ideas why that does not work?
Am I missing the RewriteBase or something?
Thanks in advance
Alex
As the subdomain already points to a subfolder, it has to be the the according subfolder below that one in the directory definition. In addition I changed the regexp as well.
Here is what worked for me:
<Directory />
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*\/api(\/.*)$ /api/index.php?url=\/$1 [QSA,L]
</Directory>

I just can't start Laravel on Apache2/Ubuntu. Give me some suggestions

Am trying to get to the laravel welcome screen while running Apache on Ubuntu.
Here are the steps I take:
1) I download composer.phar file
2) I create new laravel project with:
composer create-project laravel/laravel --prefer-dist
Now I have laravel project in my var/www/html folder.
3) Now I create a virtual host file called myapp.conf in apache2/sites-available folder.
Content of that file is as follows:
<VirtualHost *:80>
# Host that will serve this project.
ServerName app.dev
# The location of our projects public directory.
DocumentRoot /var/www/html/nino-laravel-project/public
# Rewrites for pretty URLs, better not to rely on .htaccess.
<Directory /var/www/html/nino-laravel-project/public>
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
</Directory>
</VirtualHost>
Now when I type app.dev in my browser all I get is Apache's welcome screen.
Note that I have installed all other requirements like PHP5, crypt etc..
What am I missing here?

Apache configuration for AngularJS with html5Mode enabled accessing PHP REST api outside DocumentRoot, how to?

I have the following httpd structure:
c:/
xampp/
htdocs/
mysite/
bower_components/
app/
index.html
app.js
views/
api/
index.php
.htaccess
Inside the app folder I have my application code: HTMLs, JSs, CSSs, etc. In other words my frontend.
Inside the api folder I have my PHP REST api: index.php. In other words my backend.
As said I am using AngularJS and would like to enable html5Mode, so I can have a cleaner URL.
I manage to put just the frontend to work with the following configuration in Apache:
NameVirtualHost *:8081
<VirtualHost *:8081>
ServerName localhost:8081
DocumentRoot "C:/xampp/htdocs/mysite/app"
Alias "/bower_components" "C:/xampp/htdocs/mysite/bower_components"
<Directory "C:/xampp/htdocs/mysite/app">
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]
</Directory>
</VirtualHost>
With the configuration above I am able to run my frontend like a charm.
http://localhost:8081 takes me to index.html and all my states in my $stateProvider are also resolved.
But my API calls fail and I don't know what to do.
I have the following REST service in my index.php:
require 'vendor/autoload.php';
// Create Router instance
$router = new \Bramus\Router\Router();
$router->get('/cart/items', function() {
echo '{"items":[ {"name":"item1"}, {"name":"item2"} ]}';
});
// Run the Router
$router->run();
When I call from my frontend:
$http({
url: '/api/cart/items',
method: 'GET'
}).then(function (response) {
// do stuff...
}, function (error) {
alert(error.data);
});
The call is falling in the rewrite rule to index.html. All my API calls are responding the index.html.
In my api folder I have the following .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
Options -Indexes
I have already tried to put an Alias in the Apache configuration for the api folder but the result was the same.
I've even tried to point my DocumentRoot to C:/xampp/htdocs/mysite. That way my calls to the API are resolved but when I access http://localhost:8081 I get the list of files and folders from mysite.
It seems so close and yet so far. Hope you guys can enlight me.

Deploying zend skeleton application on openshift server

I successfully deployed the skeleton application on openshift paas server.
Using Git I put my code in the folder /php.
And now, I can reach the application at the url:
=> http://zend-application.rhcloud.com/public/
In local I created the vitrual host :
<VirtualHost *:80>
ServerName zend.localhost
DocumentRoot C:/wamp/www/zend/public
SetEnv APPLICATION_ENV "development"
<Directory C:/wamp/www/zend/public>
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Is it possible to create a similar vhost on openshift server and access the appli with the base URL:
=> http://zend-application.rhcloud.com/ (without */public/)
Many thanks!
Ced.
Copy content of public into php directory itself. and make sure your .htaccess file is like code given below:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
And modify Application path in index.php file like below:
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/application'));
Then this will work properly. This worked for me in My site.
Create a symlink for your zf public folder and name it php:
From your project directory
first remove php folder (if it exists)
then
ln -s public/ php
then simply push your code via the rhc tool
That should make you project visible # http://zend-application.rhcloud.com/

Categories