Using public and resources folder as alias in laravel - laravel

I split a laravel project into 2 parts, backend and frontend, I created 2 repos on GitHub.
In the frontend repo, there are only 2 folders, public and resources folder.
So, when I pull the changes on frontend, I would like all files to replace in backend folder too.
------ backend folder
---- public folder -> I want to it be an alias
---- resources folder -> I want to it be an alias
----- frontend folder
---- public folder
---- resources folder
When I create an alias of the public and resources folder in frontend, laravel won't work.
If I create a symlink of those frontend folders, laravel works but index.php file doesn't work as it doesn't follow symlinked path
How can I get it working?

To change public folder directory go to root index.php
Under the $app = require_once __DIR__.'/../bootstrap/app.php'; add below line:
// set the public path to this directory
$app->bind('path.public', function() {
return __DIR__;
});
To change the resource of view folder directory go to config/view.php and change your desire path.
'paths' => [
realpath(base_path('resources/views'))
],

Related

How Change laravel public_path

I changed my laravel installation folder structure by moving all files and folders from the root directory to a new folder 'core'.
The goal is, to have only the folders in the root directory of the project
core
public
I have updated the index.php in public folder but I have the following problem:
when I die dump the paths, all are correct except the public path
dd(app_path()); // somepath\project\core\app
dd(storage_path()); // somepath\project\core\storage
dd(base_path()); // somepath\project\core
dd(public_path()); // somepath\project\core\public
I want to retain all other paths and change only the public path to somepath\project\public instead of somepath\project\core\public , how can I do this, online docs I've reference focus on changing public_html to public but I don't want to change the public_html, just to tell laravel that the public path is somepath\project\public
This particularly needful for me as I use the laravel storage for handling uploads.
Below is my folder structure
project
|-core
|- every other folders and files
|-public
|-every other asset files
You need to rewrite the base path in the app/Providers/AppServiceProvider.php like so:
public function boot()
{
$this->app->bind('path.public', function () {
return dirname(getcwd(), 1) . '/public';
});
}

Laravel App Not Loading CSS From Public Installation Not In public_html

I've installed a pre-made Laravel app and followed the documentation to extract it to the public root except in the documentation they are installing it into public_html, but I have it in a subdomain in a folder named "test" which is on the same level as public_html.
All the CSS and JS should be loading from the public subdirectory, but they try to look for it in the main root folder.
For example, this file returns a 404 error:
https://test.mydomain.com/css/style.css
The file is actually at this location:
https://test.mydomain.com/public/css/style.css
The absolute path of the file is:
/home/mydomain/test/public/css/style.css
Instead of:
/home/mydomain/public_html/public/css/style.css
What environment or config file isn't set correctly and how do I set it to use the correct directory?
UPDATE
Changing the ASSET_URL to "public" fixed the first issue, but now the same thing is happening with the admin.
The app tries to load the dashboard with this URL:
https://test.mydomain.com/admin/dashboard
The page loads all the assets normally when I remove the word "admin" from the URL:
https://test.mydomain.com/dashboard
But this doesn't work for all the pages.
You can try to put public directory path in below given conflagration file.
Config File Path : config/app.php (asset_url)
'asset_url' => env('ASSET_URL', 'https://test.mydomain.com/public/')

How to host laravel web application in Shared Hosting

I have developed a web application using Laravel 5.8, now I want to host that to a shared web server. What will be the folder structure and exact what to configure.
I have created a folder named laravel and kept all the project folders into that laravel folder, except project's public folder. I kept all the files and folders of public folder into public_html folder.
I have configured the index.php file of public_html folder as below.
a. require __DIR__.'/../bootstrap/autoload.php';
to,
require __DIR__.'/../laravel/bootstrap/autoload.php';
b. $app = require_once __DIR__.'/../bootstrap/app.php';
to,
$app = require_once __DIR__.'/../laravel/bootstrap/app.php';
$app->bind('path.public', function() {
return __DIR__;
});
*** I have solved the problem myself.
Step 1: I have found and installed laravel app into public_html folder from the cPanel
Step 2: Then I have moved everything into a folder named laravel except public folder into a new folder outside public_html folder
Step 3: Then I have moved everything from public folder into public_html folder
Step 4: Later I have followed the 2nd step above
Step 5: Then I have changed folder permission for storage folder and it works fine.
Step 6: Finally I have kept all other files and folders of the app developed in my local server into this newly installed app directories
Laravel default structure always better. its depend on your hosting place.
if your hosting place support for laravel framework then use default structure automatically public word redirect to project path.
Or if you need to ignore Public word in your Url
Copy all files form files put in to one folder ex:psl
copy public folder paste in to folder directory
change the index.php file in two place
require __DIR__.'/psl/bootstrap/autoload.php';
$app = require_once __DIR__.'/psl/bootstrap/app.php';
I had similar errors on my deploy and then I noticed that the path is kind of wrong:
require __DIR__.'/../laravel/bootstrap/autoload.php';
the leading / changes to the root of the drive, and then the .. are not doing anything. Removing that leading / on both require __DIR__ fixed it for me.
if you can not provide a link to the web root of the shared folder!
All except the Public folder to move to a higher level, such as a folder laravel - http://prntscr.com/bryvu7
(Maybe on hosting your folder - public. not a folder public_html)
Change file publi_html/index.php
line
require __DIR__.'/../bootstrap/autoload.php';
to
require __DIR__.'/../laravel/bootstrap/autoload.php';
And line
$app = require_once __DIR__.'/../bootstrap/app.php';
to
$app = require_once __DIR__.'/../laravel/bootstrap/app.php';
$app->bind('path.public', function() {
return __DIR__;
});
Change file laravel/server.php
line
require_once __DIR__.'/public/index.php';
to
require_once __DIR__.'/index.php';

remove Laravel 4.2 /public from url

I uploaded my laravel 4.2 project on my shared webhost. now I have to go to mydomain.com/public to see the view . But I dont want ant public folder in my address . I used so many suggested .htaccess files but they didn't work for me.
Does any one know other solutions ? (not using .htaccess file) ... for example any laravel config that can help me...
thanks
Normally you need the htaccess file to point to the public folder. But you can copy the content of the public folder in your root folder and change the app path to it.
Copy your index file in your root folder and add this to your index.php file:
// set the public path to this directory
$app->bind('path.public', function() {
return __DIR__;
});
Here is solution I recently found and worked on my local host (I wish and think this works on shared webhost too). I hope it helps someone if has the same problem :
laravel 4 remove public from the URL
Follow the steps below to achieve this.
Step 1
Move everything from public folder to the root directory
Step 2
Now delete the empty ‘public’ folder.
Step 3
Edit the file bootstrap/paths.php
Find the following line
'public' => __DIR__.'/../public',
and replace it with:
'public' => __DIR__.'/..',
Step 4
Modify index.php in root
Find the following line
require __DIR__.'/../bootstrap/autoload.php';
and replace it with:
require __DIR__.'/bootstrap/autoload.php';
Now in the same file find the following line
$app = require_once __DIR__.'/../bootstrap/start.php';
and replace it with:
$app = require_once __DIR__.'/bootstrap/start.php';
That’s all. Browse to your Laravel installation without public in the URL and make sure it works.

Uploading Laravel Project onto Web Server

I am trying to upload my Laravel project onto my web server, but my past two attempts have been unsuccessful. I believe I am not uploading the files to the right location.
This is my web server's structure ->
Am I correct to say that I need to upload ALL of my laravel files into public_html?
This is my Laravel project's directory :
EDIT : I have now added all the files onto the root folder, and public into public_html, however none of my routes seem to work. (They work perfectly on localhost). Everything throws a 404
No, but you have a couple of options:
The easiest is to upload all the files you have into that directory you're in (i.e. the cPanel user home directory), and put the contents of public into public_html. That way your directory structure will be something like this (slightly messy but it works):
/
.composer/
.cpanel/
...
app/ <- your laravel app directory
etc/
bootstrap/ <- your laravel bootstrap directory
mail/
public_html/ <- your laravel public directory
vendor/
artisan <- your project's root files
You may also need to edit bootstrap/paths.php to point at the correct public directory.
The other solution, if you don't like having all these files in that 'root' directory would be to put them in their own directory (maybe 'laravel') that's still in the root directory and then edit the paths to work correctly. You'll still need to put the contents of public in public_html, though, and this time edit your public_html/index.php to correctly bootstrap the application. Your folder structure will be a lot tidier this way (though there could be some headaches with paths due to messing with the framework's designed structure more):
/
.composer/
.cpanel/
...
etc/
laravel/ <- a directory containing all your project files except public
app/
bootstrap/
vendor/
artisan
mail/
public_html/ <- your laravel public directory
I believe - your Laravel files/folders should not be placed in root directory.
e.g. If your domain is pointed to public_html directory then all content should placed in that directory. How ? let me tell you
Copy all files and folders ( including public folder ) in public_html
Copy all content of public folder and paste it in document root ( i.e. public_html )
Remove the public folder
Open your bootstrap/paths.php and then changed 'public' => __DIR__.'/../public', into 'public' => __DIR__.'/..',
and finally in index.php,
Change
require __DIR__.'/../bootstrap/autoload.php';
$app = require_once __DIR__.'/../bootstrap/start.php';
into
require __DIR__.'/bootstrap/autoload.php';
$app = require_once __DIR__.'/bootstrap/start.php';
Your Laravel application should work now.
If you are trying to host your Laravel app on a shared hosting, this may help you.
Hosting Laravel on shared hosting #1
Hosting Laravel on shared hosting #2
If you want PHP 5.4 add this line to your .htaccess file or call your hosting provider.
AddType application/x-httpd-php54 .php
In Laravel 5.x there is no paths.php
so you should edit public/index.php and change this lines in order to to pint to your bootstrap directory:
require __DIR__.’/../bootstrap/autoload.php’;
$app = require_once __DIR__.’/../bootstrap/app.php’;
for more information you can read this article.
Had this problem too and found out that the easiest way is to point your domain to the public folder and leave everything else the way they are.
PLEASE ENSURE TO USE THE RIGHT VERSION OF PHP. Save yourself some stress :)
All of your Laravel files should be in one location.
Laravel is exposing its public folder to server. That folder represents some kind of front-controller to whole application. Depending on you server configuration, you have to point your server path to that folder. As I can see there is www site on your picture. www is default root directory on Unix/Linux machines.
It is best to take a look inside you server configuration and search for root directory location. As you can see, Laravel has already file called .htaccess, with some ready Apache configuration.

Resources