Laravel add old path when I try to use a resource - php

I created some resources with php artisan, located in Http/Resources/Api/V1
Then I moved all at the root folder : Http/Resources, and deleted the Api/V1 folders
But now, my application uses an old version of my resources, I dont know why.
For example, in my controller I have :
public function show($id)
{
return new UserResource(User::findOrFail($id));
}
But the resource used is an old one, it doesn't even exists anymore

When you moved your resources from Http/Resources/Api/V1 to Http/Resources make sure you change the namespace in the resource files to reflect those changes.
After changing the namespaces make sure you use the correct includes at the top of your controllers because they will be different after you changed your namespaces.
If you did all the above steps make sure to run:
composer dump-autoload
This will fix any autoloading issues after you changed namespaces.

Related

How laravel's helper functions are available globally in every file?

I have just started exploring laravel but I have one confusion . I know how to create own custom function file and make it available globally using compose.json file but I was trying to figure out how laravel's helper function like route() , view() are accessible without including there source file and I can't find any auto discovery in composer.json file neither in any Service Provider .
PS : I have only checked in in Providers/ Directory.
Can anyone tell me how this thing works?
Through composer Laravel has defined which files should be autoloaded. With the line in the composer.json file in Laravel/framework it specifies what should be autoloaded.
It loads the following file.
You can create similar autoloaders if you prefer, but having to much logic in such helpers could easily become an anti pattern. As the logic, is a little more hidden than class based logic, when people have to look through your projet.
On your composer.json file on the root directory of your laravel app, look for the entry autoload.
This means all methods on those directories are autoloaded.
That is why if you have (newly) created a method / function within those directory and it doesn't work (or not found) as expected, you need to run composer dump-autoload to make sure that everything has been loaded.
That's also where I put my custom helper file:
"files": [
"app/Helpers/helpers.php"
]
All function here will then be available on all controllers, traits and views.

Is there no manual include of Laravel?

I was starting some .php files in the /public directory of Laravel, which works, naturally, but it is separated from the standard Laravel system. In fact, Laravel wants you to you routes I know, and if I want to use some Laravel stuff I would think that calling
require_once 'path/to/vendor/autoload.php';
require_once 'path/to/app/Services/Myfile.php';
would let one use that... but for example "use GuzzleHttp", if used in myfile, gets fatal "Uncaught ReflectionException: Class config does not exist" in Container.php of Laravel.
I know there's SHORTINIT in Wordpress though it is kind of a mess of requiring any file that you need and all the files with functions it uses... Is there something similar for Laravel? Or this is never properly used this way to hold php files within /public?
You don't need to use require as composer takes care of loading the library files.
However when you add any new library or make any changes to routes or config files make sure you run the optimize command.
php artisan optimize

Laravel: How to include file from Vendor folder in Laravel

I am trying to include the YouTube Analytics Service of Google but I can not access it through the Vendor folder.
include(app_path.'path/to/analytics/Google_YoutubeAnalyticsService.php')
It is not working, because it defaults to the App folder.
How can I get out of the App folder and into the Vendor folder (where the YouTube Analytics file is at)?
The error is {
include(C:\xampp\htdocs\mysite\app/path/to/analytics/Google_YoutubeAnalyticsService.php):
failed to open stream: No such file or directory
From where do you want to include that file ?
Place a reference to your file in composer.json autoload object:
"autoload": {
"files":["your_file_path"]
}
Run composer dumpautoload, and you'll have your file :)
Actually you have in the helpers function the path so basically the function base_path give the direction to the root of your project so
echo base_path() . '/vendor';
Should be the route to your vendor folder.
You can se all the documentation in
Helper Functions Laravel
Be sure that you are seeing the documentation of the laravel version that you are using (I put the link for the 4.2 version).
This question was asked a long time ago and the answers reflect that. Most the time now all you need to do is import it using the "use" statement if you installed it with composer. Composer will already reference all the important directories.
It should be something like this, but it will vary depending on the project.
use FolderNameUsuallyGitHubUserName\ClassNameorGitHubProjectName\Class;
That could include a base class as well as some exception classes.
use FolderNameUsuallyGitHubUserName\ClassNameorGitHubProjectName\ClassException;
Usually most packages if compliant with modern composer and php standards work in this fashion.

Laravel 4 - class not found on live server

I'm uploading my laravel 4 app to a testing server which is shared hosting.
I'm uploading to a password protected directory which has a .htaccess file within it. My subdomain points to the public folder.
For the most part they app is working as expected I can log in, view most of the pages however every class that I have created such as a helper class and additional controllers are not being found on the live server yet all works on my local environment.
I've redone a composer dump-autoload and uploaded the composer.json file
I'm not sure where to start with this.
In my upload I've included all the files and folders to the live server (twice now). I read somewhere else I should namespace my classes but why would this help if the main laravel controllers do not namespace?
Confused - all help appreciated
When you do a composer update, if Composer finds anything new it will update some files in the folder
vendor/composer
Like the file autoload_classmap.php.
So, you have to reupload at least this folder too.
Maybe it's about git, you are pushing changings with some certain case sensitive Folders and Files, but git is changing this therefore it will work on Mac and Windows OSs but not on the server.
just use this command:
git config core.ignorecase false
above command for the current repository and you may add --global just after config keyword.
please note that ignorecase option available since version 1.5.6 and I assume you are running 2.0.x but just mentioning!

Override pear Package

I'm using Math_Finance pear package locally. I made a modification to some file included in the package, this made my calculations work. Now I migrate my project to a web shared hosting and asked to install the same pear package. However I'm unable to perform the same modification I did locally because is a shared hosting.
My question is: is there a way to override or just include the file that was modified? I don't want to copy all files in the package to my public_html directory but I'm afraid that this is the only solution.
Thanks
You could send in a patch if it's a bug fix. If it's just added functionality. You could simply overwrite some functionality of the Math_Finance class by extending it. (thanks for the tip #hek2mgl ;)
Class Math_Finance {
public function someMethod() {
// original logic
}
}
Class My_Math_Finance extends Math_Finance {
public function someMethod() {
// change some of the needed logic
}
}
Depending on the method they used for representing the path to the included files this may work.
Assuming the original file is in
/usr/local/share/php/PEAR/Statistics/Cool.php
In your site mirror the PEAR and save your version as
/mywebroot/library/PEAR/Statistics/Cool.php
Then prepend the path to your library directory into the include paths
ini_set("include_path", "/mywebroot/library".PATH_SEPARATOR.ini_get("include_path"));
Now when including your version will be found prior to PEAR's.
I don't want to copy all files in the package to my public_html directory but I'm afraid that this is the only solution.
Yep! that's the solution. But what is so bad with that? We are talking about 2 additional php files ;) :
Math/Finance.php
Math/Finance_FunctionParameters.php
Place them in a folder inside your application, let's say lib. Then make sure, that you import both files from this location:
require_once 'lib/Math/Finance.php';
require_once 'lib/Math/Finance_FunctionParameters.php';
I had issues with pear as well and had to change to remove the pear requirement - here is the fork from original repo without pear
https://github.com/hashmode/Math_Finance

Categories