How to change the Public directory path in Lumen? - lumen

By default Lumen "same as Laravel" has myApp/public directory to put all the public files (assets).
I want to change that directory path from myApp/public to myApp/src/custom/public. How can I do achieve this?

I also struggled with this and found a soltuion elsewhere.
I wanted the following directory structure:
.hosting root dir
├── lumen_app_dir <---
├── other_app_dir
├── etc...
└── public_html
└──lumen_app_public_dir <---
So I did the following:
Copied the index.php and .htaccess from lumen_app_dir/public to the lumen_app_public_dir.
Changed the index.php there like this:
$app = require __DIR__.'/../../lumen_app_dir/bootstrap/app.php';
$app->run($app['request']);
The important part here is, that I had to include $app['reqest'] as the parameter for run function.
And it just works without any change in the default .htaccess file. I can access the Lumen installation at server.dev/lumen_app_public_dir

Got the same problem, and solved it. Look here, maybe it will help you.
I have done this solution for my Lumen application, which works for me.
UPDATE
Ok, let's go proceeding some changes to make the system to work with your tree.
Add a .htaccess file in the root of your application so in the directory myApp\. Write it in :
RewriteEngine On
RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]
RewriteRule ^((?!public/).*)$ src/custom/public/$1 [L,NC]
Assuming that you have configured your vhost pointing to \Path\myApp, we now accede to the index file of myApp\src\custom\public\. If we didn't make any mistakes, then we should get to a page that indicates an error, telling us that the bootstrap/app.php file is not found. Logic.
We must therefore change the index.php file in the directory myApp\src\custom\public :
Change from this :
$app = require __DIR__.'/../bootstrap/app.php';
To this :
$app = require __DIR__.'/../../../bootstrap/app.php';
You can now get your home page directly from the path wanted.

You can override your public directory using the IoC container like this:
App::bind('path.public', function()
{
return base_path().'/public_html';
});
But I prefer to use a symlink to the public folder like this:
ln -s public public_html

I believe you should "point" your vhost to the new location (assuming you moved/renamed the public dir to your desired path). This is only needed is your server is not yet configured this way.
Then open the index.php from your new location and "fix" the path to bootsrap/app.php
Then open server.php from the Lumen base dir and edit your paths in both locations you find "public" mentioned.
Didn't really test this but looks like it should work. Give it a try.

One solution is to bind the path fro public in the appServiceProvider.php register method:
$app->bind('path.public', function() {
return __DIR__; });
A reliable way is to go into your public_html/index.php!
I'm using Lumen 6.x and it's work properly.

Related

codeigniter 3.1.9 with apache and php 7.2 - routing/redirect issues with symbolic links

SCENERIO:
Recently I migrated CodeIgniter from 2.4 to 3.1.9 along with PHP from 5.2 to 7.2. Also, created a new httpd.conf file in apache. When I go to the site's URL, it loads the default page fine and I can browse to default or root
pages without any issues. There are multiple sub-domains that are mapped in routes.php to the main httpd root directory /var/www/html. These sub-domains have a symbolic link to /var/www/html, and within assets folder are
the customization files for each sub-domains.
Root HTTPD directory "/var/www/html" structure
application assets
test1 -> /var/www/html
test2 -> /var/www/html
index.php
system
URL is set as follows in config.php.
$config['base_url'] = 'https://example.com/';
Below is my entry in routes.php:
$route['default_controller'] = "home";
$route['404_override'] = '';
$route['home'] = "home";
$route['select-product'] = "home/select-product";
$route['test1/select-product'] = "home/select-product";
$route['test2/select-product'] = "home/select-product";
Please check the below image for httpd directory config:
enter image description here
PROBLEM:
When I browse to https://example.com/ it loads everything correctly.
When I go to https://example.com/test1 ==> First page loads correctly. But when I press next, the second page defaults back to 'https://example.com/';. Somehow, it looses /test1 from the browser.
Not sure if this is caused by CodeIgniter URL routing issue or something to do with httpd.conf file of Apache. The same "routes.php" file was working fine on the old server.
The routing/pagination seems to have broken after I set $config['base_url'] = 'https://example.com/';. If left empty, it takes a while for the site to load, and when it does the forms and images are not loaded correctly.
I have spent lots of hours trying to figure out the problem. I am hoping someone genius out there will be able to show be in the right direction.
Thank you.
I had this problem when I upgraded to PHP 7.2, the best solution is ,
for things to work in CI with PHP7.2,
Find and comment out(or remove) the following code from sessions.php (system/libraries/session/session.php) and place it in your index.php at the top.
session_start();
ini_set('session.use_trans_sid', 0);
ini_set('session.use_strict_mode', 1);
ini_set('session.use_cookies', 1);
ini_set('session.use_only_cookies', 1);
ini_set('session.hash_function', 1); ini_set('session.hash_bits_per_character',4);
and for .htaccess, use this
DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|.well-known|images|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1
set your uri protocol to AUTO
$config['uri_protocol'] = 'AUTO';
$config['index_page'] = '';

How to run custom php script from doc root in magento2

I wanted to add custom php script to magento2 root folder and run from the browser.I tried to add it in magento2 root folder but it redirects to 404 page.
I also tried to add it in pub folder but no success.
Also cleared cache and empty generation folder.
I am using nginx server
If you are using nginx configuration that comes with magento you need to put a file inside pub folder to allow access to it from the browser as pub is the document root of the vhost. Magento root dir is one level up. Second of all default config for nginx allows only to access index.php, get.php, static.php, report.php, 404.php and 503.php files. Any other are not processed by the php. You can see this in line with location ~ (index|get|static|report|404|503)\.php$ { in nginx.conf.sample. If you are not using it check your config for similar rule. To allow another file to be accessible from browser simple add another name after 503 or change entire brackets with location ~* \.php$ {
Source: https://magento.stackexchange.com/a/97290/1883
For example you can get product name in custom script by this step
step 1: create index.php at root of magento 2
magento2/test/index.php
<?php
require __DIR__ . '../../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('customScript');
$bootstrap->run($app);
step 2: create customScript.php
magento2/test/customScript.php
<?php
class customScript
extends \Magento\Framework\App\Http
implements \Magento\Framework\AppInterface {
public function launch()
{
$this->_state->setAreaCode('frontend'); //Set area code 'frontend' or 'adminhtml
$id = 12;
$_product = $this->_objectManager->create('\Magento\Catalog\Model\Product')->load($id);
echo $_product->getName();
return $this->_response;
}
public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
{
return false;
}
}
Now you can run this custom script by
http://10.16.16.196/magento2/test/
As stated by #Ranjit, the /pub folder must be your Magento root folder.
The correct way to run a standalone php script on Magento would be:
On nginx:
Locate location ~ ^/(index|get|static|errors/report|errors/404|errors/503|health_check)\.php$ { and add your file there.
I.e:
location ~ ^/(index|get|static|errors/report|errors/404|errors/503|health_check|myphp)\.php$ {
Then you can access yourstore.com/myphp.php.
On Apache:
Simply add the file under /pub folder. I.e.: /pub/myphp.php.
Apache rewrites rule will redirect to index.php if the file or folder doesn't exist.
In my case on Apache (cPanel) the problem was that file permissions of .php files should not be writable by group or others to be served directly, else Magento's 404 would open.
So in my case to serve file directly I had to set file permission to -rw-r--r-- (on Linux).
This is the mostly issue with servers using suPHP.
Putting this here in case someone runs into same situation...

Nginx - How to redirect a directory to a file that its path contains the directory?

I'm new to Nginx and I'm trying to redirect a directory to a file, here is basically what I'm trying to do:
When entering this link:
http://localhost:8889/dir
go to this link instead:
http://localhost:8889/dir/path/to/the/file/index.html
As you can see the directory is part of the file path which I'm trying to redirect to. Plus there is already a location block in the config file to redirect that directory to the proper location:
location /dir {
root /opt/dir;
}
My first attempt was to use the rewrite directive as I saw from this blog(https://jeffsebring.com/2012/nginx-301-redirects/):
if ( $request_filename ~ dir ) {
rewrite ^ http://localhost:8889/dir/path/to/the/file/index.html permanent;
}
But the page says it has a redirect loop, which I believe is the conflict with the old location block.
Then I tried to add another location block as I saw from here(nginx rewrite virtual directory to file):
location /dir {
rewrite ^/dir$ /dir/path/to/the/file/index.html;
}
But after reloading config file, Nginx told me there is already a location block with the same directory exists.
So my question is is there any way I can do this? or it is not even possible?
Thanks!!
Found the solution.
I used "return" to return the full hard-coded url instead rewrite the current one:
location ~* /dir$ {
return http://localhost:8889/dir/path/to/the/file/index.html;
}
And also this rewrite solution works as well:
location ~* /dir$ {
rewrite ^/dir$ /dir/path/to/the/file/index.html;
}
The problem I had was actually caused by cache. I was actually reloading the page from cache so I could not see the result after changing nginx config file, and I resolved this by checking the "Disable cache" option in developer tools in Chrome.

PHP Built-in Webserver and Relative Paths

TL;DR
Does PHP 5.4 built-in webserver have any bug or restriction about relative paths? Or does it need to be properly (and additionally) configured?
When I used to programming actively I had a system working under URI routing using these lines in a .htaccess file:
RewriteEngine On
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php [L]
The FrontController received the Request, find the proper route from given URI in a SQLITE database and the Dispatcher call the the Action Controller.
It worked very nicely with Apache. Today, several months later I decided to run my Test Application with PHP 5.4 built-in webserver.
First thing I noticed, obviously, .htaccess don't work so I used code file instead:
<?php
if( preg_match( '/\.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"] ) ) {
return false;
}
include __DIR__ . '/index.php';
And started the webserver like this:
php.exe -c "php.ini" -S "localhost:8080" "path\to\testfolder\routing.php"
So far, so good. Everything my application need to bootstrap could be accomplished by modifying the include_path like this:
set_include_path(
'.' . PATH_SEPARATOR . realpath( '../common/next' )
);
Being next the core folder of all modules inside a folder for with everything common to all applications I have. And it doesn't need any further explanation for this purpose.
None of the AutoLoader techniques I've ever saw was able to autoload themselves, so the only class manually required is my Autoloader. But after running the Test Application I received an error because my AutoLoader could not be found. o.O
I always was very suspicious about realpath() so I decided to change it with the full and absolute path of this next directory and it worked. It shouldn't be needed to do as I did, but it worked.
My autoloader was loaded and successfully registered by spl_autoload_register(). For the reference, this is the autoloading function (only the Closure, of course):
function( $classname ) {
$classname = stream_resolve_include_path(
str_replace( '\\', DIRECTORY_SEPARATOR, $classname ) . '.php'
);
if( $classname !== FALSE ) {
include $classname;
}
};
However, resources located whithin index.php path, like the MVC classes, could not be found. So I did something else I also should not be doing and added the working directory to the include_path. And again, manually, without rely on realpath():
set_include_path(
'.' . PATH_SEPARATOR . 'path/to/common/next'
. PATH_SEPARATOR . 'path/to/htdocs/testfolder/'
);
And it worked again... Almost! >.<
The most of Applications I can create with this system works quite well with my Standard Router, based on SQLITE databases. And to make things even easier this Router looks for a predefined SQLITE file within the working directory.
Of course, I also provide a way to change this default entry just in case and because of this I check if this file exist and trigger an error if it doesn't.
And this is the specific error I'm seeing. The checking routine is like this:
if( ! file_exists( $this -> options -> dbPath ) ) {
throw RouterException::connectionFailure(
'Routes Database File %s doesn\'t exist in Data Directory',
array( $this -> options -> dbPath )
);
}
The dbPath entry, if not changed, uses a constant value Data/Routes.sqlite, relatively to working directory.
If, again, again, I set the absolute path manually, everything (really) works, the the Request flow reached the Action Controllers successfully.
What's going on?
This a bug in PHP's built-in web server that is still not fixed, as of PHP version 5.6.30.
In short, the web server does not redirect to www.foo.com/bar/ if www.foo./bar was requested and happens to be a directory. The client being server www.foo.com/bar, assumes it is a file (because of the missing slash at the end), so all subsequent relative links will be fetched relative to www.foo.com/instead of www.foo.com/bar/.
A bug ticket was opened back in 2013 but was mistakenly set to a status of "Not a Bug".
I'm experiencing a similar issue in 2017, so I left a comment on the bug ticket.
Edit : Just noticed that #jens-a-koch opened the ticket I linked to. I was not awar of his comment on the original question.

How to change include directory of Zend Framework

I get the following error messages:
Warning: include_once(Zend\Db.php) [function.include-once]:
failed to open stream: No such file or directory in
C:\EasyPHP3\www\VPZ\Lib\Zend_1.7.7\Loader.php on line 83
Warning: include_once() [function.include]:
Failed opening 'Zend\Db.php' for inclusion (include_path='VPZ/') in
C:\EasyPHP3\www\VPZ\Lib\Zend_1.7.7\Loader.php on line 83
Warning: require_once(Zend/Exception.php)
[function.require-once]: failed to open stream:
No such file or directory in
C:\EasyPHP3\www\VPZ\Lib\Zend_1.7.7\Loader.php on line 87
Fatal error: require_once() [function.require]:
Failed opening required 'Zend/Exception.php' (include_path='VPZ/') in
C:\EasyPHP3\www\VPZ\Lib\Zend_1.7.7\Loader.php on line 87
i want to include ZendXXX\Db.php
how to change it
create a directory (say 'lib'), and put your Zend directory in it. so your directory structure looks like this:
- application
- lib
|- Zend
- wwwroot
|- index.php
now you should add lib to your include path. edit your index.php file:
$includePath = array();
$includePath[] = '.';
$includePath[] = './../application';
$includePath[] = './../lib';
$includePath[] = get_include_path();
$includePath = implode(PATH_SEPARATOR,$includePath);
set_include_path($includePath);
now you have your lib in your include path. you can include all Zend components like this:
include 'Zend/Loader.php';
require_once 'Zend/Db.php';
the best way is too include Zend_Loader first and then use it to load classes. do this:
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Db');
you can also register to autoload classes. just add this line to your code after all those before:
Zend_Loader::registerAutoLoad('Zend_Loader',true);
now you do not need to include files to call classes. just instanciate your classes:
$session = new Zend_Session_Namespace('user');
there is no need to include 'Zend/Session/Namespace.php'.
Use set_include_path(). See PHP.net documentation
Example:
set_include_path(get_include_path() . PATH_SEPARATOR . '/path/to/Zend');
I usually store the framework files under a "library" folder:
application
public_html
library
Zend
Common
etc....
and then in my bootstrap file, or front controller, I add that "library" folder to the include path:
set_include_path(get_include_path() . PATH_SEPARATOR . '../library');
See also:
Choosing Your Application's Directory Layout.
Create the Filesystem Layout.
The reason the other suggestions say anything about doing that, is because it's a bad move - in other words, you're doing it wrong.
You can create a subdirectory and name it Zendxxx, but then you have to add that to your include_path, and change it, whenever you put a newly named version up.
I'd hazard a guess, and say that you don't have a good way to test the website (so you want to lock it to a particular version of ZF), and further, that you aren't using revision control, so you want all the previous versions of code in the site-directory to be able to go back to, if you find a problem when you change the live-running code directly on the server.
project without library And including library from one location
project C:\xampp\htdocs\my\application
library C:\xampp\Zend\library
make changes in index.php
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR,
array(realpath(APPLICATION_PATH.'/../../../Zend/library'),get_include_path(),)));