Moving laravel 5.3 from local to live - laravel

What files do I need to change to get it running on live?
Right now this is my structure:
website.com/barber_base
In barber_base I have all the laravel files except public.
website.com/public_html/barber
In barber I have only the public content.
I searched on stack but I can't find a explanation for laravel 5+.
EDIT
I edited server.php in barber_base from :
if ($uri !== '/' && file_exists(__DIR__.'public/'.$uri)) {
return false;
}
require_once __DIR__.'public/index.php';
to
if ($uri !== '/' && file_exists(__DIR__.'../public_html/barber/'.$uri)) {
return false;
}
require_once __DIR__.'../public_html/barber/index.php';
But it is not working. I get a 500 error.

You need to change in below files
\config\app.php
'url' => 'http://localhost:8888',
\config\session.php
'domain' => null, // default will be null but sometimes session creates problem. If So then need to change in this section also.

Related

How to change public folder to public_html in laravel 8?

I wanna deploy my application on shared hosting on Cpanel where the primary document root has public_html but Laravel project public
You have to follow 2 steps to change your application's public folder to public_html then your can deploy it or anything you can do :)
Edit \App\Providers\AppServiceProvider register() method & add this code .
// set the public path to this directory
$this->app->bind('path.public', function() {
return base_path().'/public_html';
});
Open server.php you can see this code
if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
return false;
}
require_once __DIR__.'/public/index.php';
Just Replace it with :
if ($uri !== '/' && file_exists(__DIR__.'/public_html'.$uri)) {
return false;
}
require_once __DIR__.'/public_html/index.php';
Then serve your application with php artisan serve, you also can deploy it on your Cpanel shared hosting where primary document root public_html
You can rename your public directory to whatever you want and tell Laravel to use the current directory path as the public path. Just navigate to the index.php inside the public folder (or whatever you renamed it to) and add the following code after the $app definition:
$app = require_once __DIR__.'/../bootstrap/app.php';
/* *** Add this code: *** */
$app->bind('path.public', function() {
return __DIR__;
});
/* ********************** */
there, idk if you have same problem like me, my cases is i using shared hosting, and i deploy it in my main domain. i place all my files in the root, my problem is the storage:link keep between public, not public_html (because its default by the hosting) so what i need to do i changhe the link using this code :
Before :
'links' => [
public_path('storage') => storage_path('app/public'),
],
After :
'links' => [
app()->basePath('public_html/storage') => storage_path('app/public'),
],
I hope it can help few people :)
Just Rename the "public" folder to "public_html" and it will work.
No changes are required in the code. Tested in Laravel 8.
my two cents :) What helped to me was:
Open LaravelService/vendor/laravel/framework/src/Illuminate/Foundation/Application.php and change publicPath() method to return public_html.
public function publicPath()
{
return $this->basePath.DIRECTORY_SEPARATOR.'public_html';
}
Then if you are using webpack also change the output folder:
const output = 'public_html';
mix.ts('resources/js/web/App.ts', output + '/js/web').setPublicPath(output).react();
This helped to me. Only issue is that it is probably not recommended to change Application.php as it is part of Laravel framework and after updating it, it will be probably erased so you have to put it back.

Laravel, getting uploaded file's url

I'm currently working on a Laravel 5.5 project, where I want to upload files, and then, I want to get their url back of the file (I have to use it client side).
now my code looks like this:
public function pdfUploader(Request $request)
{
Log::debug('pdfUploader is called. ' . $request);
if ($request->hasFile('file') && $request->file('file')->isValid()) {
$extension = $request->file->extension();
$fileName = 'tmp' . round(microtime(true) * 1000) . '.' . $extension;
$path = $request->file->storeAs('temp', $fileName);
return ['status' => 'OK', 'path' => URL::asset($path)];
}
return ['status' => 'NOT_SAVED'];
}
It works fine, I got back the OK status, and the path, but when I want to use the path, I got HTTP 404. I checked, the file is uploaded fine..
My thought is, I should register the new url in the routes. If I have to, how can I do it dynamically, and if its not necessary what is wrong with my function?
Thx the answers in advance.
By default laravel store all uploaded files into storage directory, for example if you call $request->file->storeAs('temp', 'file.txt'); laravel will create temp folder in storage/app/ and put your file there:
$request->file->storeAs('temp', 'file.txt'); => storage/app/temp/file.txt
$request->file->storeAs('public', 'file.txt'); => storage/app/public/file.txt
However, if you want to make your uploaded files accessible from the web, there are 2 ways to do that:
Move your uploaded file into the public directory
$request->file->move(public_path('temp'), $fileName); // => public/temp/file.txt
URL::asset('temp/'.$fileName); // http://example.com/temp/file.txt
NOTE: make sure that your web server has permissions to write to the public directory
Create a symbolic link from storage directory to public directory
php artisan storage:link
This command will create a symbolic link from public/storage to storage/app/public, in this case we can store our files into storage/app/public and make them accessible from the web via symlinks:
$request->file->storeAs('public', $fileName); // => storage/app/public/file.txt
URL::asset('storage/'.$fileName); // => http://example.com/stoage/file.txt

Share variable based on route group

I have 2 versions of a site. One is located in the root URL of the site and one is using a route prefix. They use the same resources but provide different links when accessed from the prefixed route:
Route::get('/', function(){
View::share('outgoing_url','something.com');
//regular links here
});
and a few more of the above pointing to different routes or
Route::group(array('prefix'=>'tour'), function(){
View::share('outgoing_url','somethingelse.com');
//different links here
});
View::share doesn't work since it uses whatever is assigned last so I am trying to find a solution for this problem.
Also, when I use HTML::link() in the views that go through the prefix, everything still points to the root URI of the site instead of the 'tour' prefix. Is there any way to differentiate between the two? Right now I am stuck with this problem and the only solution seems to be to make identical copies of the views and controllers responding to the routes. But that approach seems stupid to say the least.
I hope I explained the problem understandably.
HTML Macro:
<?php
HTML::macro('myLink', function($url, $title = null, $attributes = array(), $secure = null)
{
if (Request::segment(1) === 'tour')
{
$url = 'tour/'.$url;
}
return HTML::link($url, $title, $attributes, $secure);
});
?>
Usage:
HTML::myLink(...);
Just use a before filter - and set it that way
App::before(function($request)
{
if (Request::segment(1) === 'tour')
{
View::share('outgoing_url','tour.com');
}
else
{
View::share('outgoing_url','other.com');
}
});

Laravel FrozenNode Administrator run in maintenance mode

My question is: How can I leave the Frozennode administrator runs normaly on Laravel Maintenance Mode?
This is what I got in global.php
App::down(function()
{
return Response::view('maintenance', array(), 503);
});
Thanks!
I've dug in the core, there's no way you can do it. Laravel checks for a file named down in app/storage/meta folder, if it's there, Laravel won't even call the routes, it'll just show the error page.
This is isDownForMaintenance function from laravel:
public function isDownForMaintenance()
{
return file_exists($this['path.storage'].'/meta/down');
}
There's no configuration possible.
An alternative way to the laravelish "maintenance mode" is to set a new value in config/app.php, add:
'maintenance' => true,
Then add this to your before filter:
App::before(function($request)
{
if(
Config::get('app.maintenance') &&
Request::segment(1) != 'admin' // This will work for all admin routes
// Other exception URLs
)
return Response::make(View::make('hello'), 503);
});
And then just set :
'maintenance' => true,
To go back to normal mode
There is actually another way, more straightforward. As you can read in Laravel documentation, returning NULL from closure will make Laravel ignore particular request:
If the Closure passed to the down method returns NULL, maintenance mode will be ignored for that request.
So for routes beginning with admin, you can do something like this:
App::down(function()
{
// requests beginning with 'admin' will bypass 'down' mode
if(Request::is('admin*')) {
return null;
}
// all other routes: default 'down' response
return Response::view('maintenance', array(), 503);
});

Codeigniter Page cache with GET parameter

I am newbie to CI cache. I am facing some weird problem with codeigniter page caching. $this->output->cache(300);
I was expecting that cached version would not load if arguments in GET[] would change. But it is loading cache without considering any GET[] parameters.
I have one page where it says whether comment has been saved or not [via get parameter],
/product/product-name/?saved=true redirecting to same page where comment form is located. But it is not working. How can i invalidate old cache and create new one depending upon the get parameter? or i need to change the behavior of my comment system?
Thanks.
EDIT
Should i simply use database cache instead of Web page cache in this case?
You just have to enable the cache_query_string option in the config/config.php file.
$config['cache_query_string'] = TRUE;
Create a cache_override hook to check if there are any GET[] variables set and then skip the cache_override.
[EDIT #1]
Here is an example:
Create this file in your hooks directory:
<?php
class GetChecker {
public function checkForGet()
{
global $OUT, $CFG, $URI;
if (isset($_GET) AND ! empty($_GET))
{
return;
}
if ($OUT->_display_cache($CFG, $URI) == TRUE)
{
exit;
}
}
}
Then add this to the config/hooks.php:
$hook['cache_override'][] = array(
'class' => 'GetChecker',
'function' => 'checkForGet',
'filename' => 'GetChecker.php',
'filepath' => 'hooks'
);
I haven't tested it, it might need a little tweaking to work...
I test on CI 3+ , file system/core/Output.php 559 line, change this
if ($CI->config->item('cache_query_string') && !empty($_SERVER['QUERY_STRING']))
{
$uri .= '?'.$_SERVER['QUERY_STRING'];
}
on this
if ($CI->config->item('cache_query_string') /* && ! empty($_SERVER['QUERY_STRING']) */ && !empty($_REQUEST))
{
// $uri .= '?'.$_SERVER['QUERY_STRING'];
$uri .= '?'.http_build_query($_REQUEST);
}
And add string to your application/config/config.php
$config['cache_query_string'] = true;
it will be work with GET, POST, COOKIE ....
If need only GET, just $config['cache_query_string'] = true; - enough
I found no easier way using Hooks to prevent writing cache, as its calling _write_cache() inside the _display() method itself of CI_Output class.
For quick and easiest solution I added two conditions to display cache and write cache, if Query String parameter has variable defined( offset in my case, as I wanted for pagination)
Edit: system/core/Output.php
Add Condition to prevent writing cache, if specific GET parameter found:
function _write_cache($output)
{
if (isset($_GET['offset']) AND ! empty($_GET['offset']))
{
log_message('debug', " Don't write cache please. As as its matching condition");
return;
}
...
...
}
Add Condition to prevent displaying cache, if specific GET parameter found:
function _display_cache(&$CFG, &$URI)
{
if (isset($_GET['offset']) AND ! empty($_GET['offset']))
{
log_message('debug', " Don't display cache please. As as its matching condition");
return FALSE;
}
...
...
}

Resources