Can't use public_path() in a helper file Laravel 5.4 - laravel

I have a helper.php file in app/Helpers directory. I included that file in composer.json:
...
"files": [
"app/Helpers/helpers.php"
]
...
Helper works fine but I can't use public_path() method there. I need to include another file (please don't ask me why because it's old code that I don't need to rewrite). So I have the following:
require_once public_path() . '/appadmin/bootstrap.php';
I know that by default Laravel looks in /public/ folder but I faced with a problem. If I need to perform composer update I have to use public/appadmin/bootstrap.php path in helper.php, but after performing I have to change that path to /appadmin/bootstrap.php for correct work. That's why I decide to use public_path() method to receive correct path for both cases. And if I use it I'm getting an error:
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postUpdate
Script Illuminate\Foundation\ComposerScripts::postUpdate handling
the post-update-cmd event terminated with an exception
[ReflectionException]
Class path.public does not exist
Thank's in advance!

Have you tried updating your app to the current revision?
There are some files in the framework itself who need to be updated.
Check out the config files, the bootstrap files, server.php and the start files here https://github.com/laravel/laravel/tree/develop.
You could open index.php (in your public directory) and change:
$app = require_once __DIR__.'/../bootstrap/app.php';
// set the public path to this directory
$app->bind('path.public', function() {
return __DIR__;
});
Now you dont need to change your public path when your public directory has changed.

Related

vendor autoload file cannot be loaded because of the public folder in codeigniter 4

I already installed GuzzleHttp using composer but the require 'vendor/autoload.php'; returns an error. I've tried using FCPATH . 'vendor/autoload.php'; and it returns C:\xampp\htdocs\local\public\vendor\autoload.php". I think the problem is the public folder in the url. When I tried it without the public folder it works.
Notes:
FCPATH
The path to the directory that holds the front controller.
ROOTPATH
The path to the project root directory. Just above APPPATH.
Solution:
Instead of:❌
require FCPATH . 'vendor/autoload.php';
Use this:✅
require_once ROOTPATH . 'vendor/autoload.php';
You could alternatively, use COMPOSER_PATH:✅
I.e:
require_once COMPOSER_PATH;
Composer Path
The path that Composer's autoload file is expected to
live. By default, the vendor folder is in the Root directory, but you
can customize that here.

Change public directory laravel

I want to change the public folder to public_html, the main problem is that some commands and functions not work because it still point to public.
I am trying to use the laravel+vue to develop a spa. i normally rename the public folder to public_html and that's all, but know it seems that i have to use the public_path helper and the console and these use the public folder too.
im using npm run dev as in the laravel docs, but it generate everything in the public folder, not in the public_html that i have renamed
so how can i tell the console and the public_path helper to do they magic in the public_html folder?
I am using Laravel 5.6, it is a fresh project, with just a controller and a view with a mix helper calling the js that i want link this <script src="{{ mix('js/app.js') }}"></script>.
May this can help you. You can override Laravel default helper methods. This code helped me at a time when I got same problem like you.
Simple I created one helper file and override public_html function something like this:
/**
* Get the path to the public folder.
*
* #param string $path
* #return string
*/
function public_path($path = '')
{
return base_path().'/public_html';
}
Let's say file name is AppHelper.php and located into app/Http folder.
To override helper from base you need to include your helper file(AppHelper.php) before Laravel does it's own autoload file.
So for the web you have to include your file into index.php like this:
require __DIR__.'/../app/Http/AppHelper.php';
require __DIR__.'/../vendor/autoload.php';
Remember your helper must be included before autoload file. and this code snippet for Command line or Artisan CLI:
require __DIR__.'/app/Http/AppHelper.php';
require __DIR__.'/vendor/autoload.php';
You have to write your artisan cli changes into artisan file which is on root.
In Laravel if you check helper function then each and every function wrapped with if condition like this function is not exist then declare it using function_exists()
In our case we already declare public_path function and included before Laravel autoload file so every request first execute our function.
Hope this can help you. Good luck.
Add this in register() method of your AppServiceProvider
public function register()
{
// Add this part
$this->app->bind('path.public', function() {
return base_path().'/public_html';
});
}
For the css+js resources, as #Bagus Tesa said, you have a file named webpack.mix.js in the root of your application folder. It's not you who created it, it comes with the laravel project. In that file you'll find something like:
mix.js('resources/assets/js/app.js', 'public/js')
.sass('resources/assets/sass/app.scss', 'public/css');
Change the destination path from public to public_html

After rename app folder and namespace artisan make shows Exception: Unable to detect application namespace

I've renamed my app folder to aplicativo and changed the namespace to aplicativo. Everything works but artisan make commands.
I changed namespace through composer.json
"psr-4": {
"aplicativo\\": "aplicativo/",
}
Plus command:
artisan app:name aplicativo
You won't get this to work. You can rename the namespace (as you did with the command), but you can't rename the directory because it's hardcoded as app.
You can see the source here.
Then... if your using composer try this command
composer dump-autoload
The good news is that Laravel has now added support for custom App path. You can override the App path by adding the following code in bootstrap/app.php (or provider).
/*...*/
$app = new Illuminate\Foundation\Application(
$_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);
// override the app path since we move the app folder to somewhere else
$app->useAppPath($app->basePath('src'));
Similarly, you can change other paths (database, language, storage, etc.). You can find all the available methods here.

How to use SimplePie with Laravel

I am using Laravel 5.2. I need to get RSS feeds using SimplePie, so I install the library through composer.json and now my vendor folder contains simplepie but when i run the following code this error shows:
ErrorException in SimplePie.php line 1379: ./cache is not writeable.
Make sure you've set the correct relative or absolute path, and that
the location is server-writable.
My code in route:
Route::get('feed', function () {
$feed = new SimplePie();
$feed->set_feed_url(array(
'http://www.thenews.com.pk/rss/2/16' //it has no images
));
$feed->init();
$itemCount=$feed->get_item_quantity();
return $itemCount;
});
Create a writeable cache folder for simplepie in the /storage/ folder (I called mine simplepie_cache).
Set the path in your feed object:
$feed->set_cache_location(storage_path() . '/simplepie_cache');
That should do the trick.
check what is saying ./cache is not writeable can't write to cache folder check permissions to this folder or try a L5 wrapper for SimplePie https://github.com/willvincent/feeds

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.

Resources