I have a REST api app using SLIM framework.
Below is my app dir structure
Below is structure in my controllers dir
Authcontroller.php is in Auth folder.
Below are namespaces given to ActionController and Authcontroller
Both controllers (Action and Auth) are defined in container as below in the mainapp
Above app is working fine on my localhost ,but when i upload same to my server, it get below error on above line 58
Please help me here , i am going mad,have changed around 3 hosting servers(SHARED-HOSTING),but still same error. PLEASE HELP!!
If your deployment server is Linux, mostly, because path is case-sensitive.
If your composer.json contains section such as below
...
"autoload": {
"psr-4": {
"App\\": "app"
},
},
...
Composer PSR-4 autoloading expect to find class
\App\Controllers\Auth\AuthController
in file
app\Controllers\Auth\AuthController.php
but it can not find it because yours is
app\controllers\Auth\AuthController.php
Since your development machine seems to use Windows which by default is case-insensitive,
app\controllers\Auth\AuthController.php
equals to
app\Controllers\Auth\AuthController.php
Which is why it works on your development machine but not on deployment server.
So the solution is to rename any directory/files to match its case and also make sure that all files is copied to deployment server.
After you change directory/filename case, run
$ composer dump-autoload
So new autoload file is generated.
Related
Laravel Project is installed in my root directory and I have another project inside root directory with Core PHP ,
COREPHP-PROJECT is the one I want to access , but every time I try to access example.com/COREPHP-PROJECT it's giving me 404 error.
I have tried more than 50 solutions from internet and nothing is working so far.
What I'm doing wrong here?
NOTE: .htaccess file is renamed as I wanted to test without it too.
I think it is actually fine to have a project directory inside the applications root directory. A good application should not be build inside the framework. It should be build with the framework. If in 5 years somehow laravel is not anymore supported you can decouple it more easy and use it in a new framework.
You just need to assign the path to your composer.json under the psr-4 part.
"autoload": {
"psr-4": {
"MainApplication\\": "app/",
"CorePhpProject\\": "CORE-PHP-PROJECT/app/",
But you should only route over the Laravel router.
You can add your routes inside laravels RouteServiceProvider.php
Have a look at the documentation:
https://laravel.com/docs/8.x/routing#the-default-route-files
Should look something like this then:
Route::prefix('coreproject')
->group(base_path('CORE-PHP-PROJECT/routes/web.php'));
Then you can call your laravel routes like this
/home
and the project one are called like /coreproject/home
I got into same task of integrating core php folder inside laravel , therefore solution i found that create a core php file with extension test.php in public folder at laravel root, and try to access in browser after php artisan serve, you will find you can access it.
While deploying my Symfony5/php7.4/MariaDB application from Github to Heroku, I have this error message : "{"message":"Uncaught PHP Exception Twig\Error\LoaderError: "Unable to find template "base.html.twig " (looked into: /app/templates, /app/vendor/symfony/twig-bridge/Resources/views/Form) in "home.html.twig"
though I do have a base.html.twig file in my project in the templates folder.
What I already did and thus do not consider the reason of the failure :
I created a procfile at the root of my projet stating web: heroku-php-apache2 public/
which is the exact name of my file in my project that is to say public/,
I added a .htaccess file at the root of my project thanks to the composer require apache-pack command,
I set my config variables in Heroku with the correct addon DB url attached by Heroku, and I set my APP_ENV as prod environement,
I set all my bundles config in my project so that they also work in prod environment,
I redeployed my application,
In my dev environment, the application runs perfectly. I have in my composer.josn file an autoload psr4 strategy as below :
"autoload": {
"psr-4": {
"App\\": "src/"
}
}
to point to the src file for the App namespace.
All files have a correct namespace.
My files directories names like "public", "templates" dot not contain capital letters, as I saw a similar post while doing research on SOF, stating that Linux used by heroku systems are case-sensitive. This does not seem to be my problem here.
I even renamed my "App" namespace into "app" namespace in every file and in my autoload psr4, as the error message mentionned "looked into: /app/templates" with no capital letter. This generated a build failed by Heroku.
Once again, this problem occured only when deploying on Heroku although I specifically set the new environement variables for the Heroku production environement.
Has anyone any idea what the cause of this error message might be? Many thanks in advance.
PS: If you rate down this question because you find that it is unclear or useless or does not show enough research - which I did though -, please, be specific and add a comment to explain the reason of your negative rate, to keep this place a qualitative one and help me understand your rating.
It finally runs well on my Heroku production environement. I did several changes. One that may explain the origin of the problem is that I used simple quotes in several templates for the extends instruction like this : "{% extends 'base.html.twig' %}, and I also noticed a blank space before ending quote in one of these templates instructions, like this : {% extends "base.html.twig " %}. I edited those instructions and replced the simple quotes by double quotes, and I erased the blank space in the template name. The application deployed correctly shortly after. This may be the answer of the Twig loading error message.
I have a simple PHP web app with the following structure:
/ (composer.json .htaccess ...)
/Core/ (Router.php, Controller.php ...)
/App/ (/Controllers, /Models, /Views ...)
/Public/ (index.php ...)
/Vendor/ (autoload.php /composer ...)
The codes runs without issue on my local server.
When I copy it to a live server and make the necessary changes to .htaccess in route I get the following error: Fatal error: Class 'Core\Router' not found in.... I have tried 3 different hosts but no luck.
The issue seems to be to do with the composer autoload function not loading in the namespaces and classes using psr-4. I have this set up in my composer.json file:
{
"autoload": {
"psr-4": {
"Core\\": "Core/",
"App\\": "App/"
}
}
The code for my project is on GitHub at
https://github.com/imoprojects/upbook
I am new to programming in an MVC structure and also with using composer in this way.
If anyone could assist with what maybe happening, I would really appreciate it.
Cheers,
Ian
You configure this:
"Core\\": "Core/",
... but your classes are at:
core
This will only work in case insensitive file systems.
I have a local project with loaded with Composer libs. I uploaded this project to my FTP and received errors, connected with not found classes from some libs.
Can I simply copy vendor/ folder to FTP or I missed something?
Error received:
Fatal error: Class 'AAA\Core\Tools' not found in /home/aaa/public_html/api.php on line 11
api.php:
<?php
use AAA\Core\Tools;
require_once("./vendor/autoload.php");
require_once("./api/" . Tools::getFieldValue('controller') . ".php");
All works fine on localhost!
Linux has a case sensitive file system. That means that files Alex.txt and alex.txt are the same thing in Windows, but not in Linux. Actually on Linux both can happily live in the same directory:
$ tree .
.
├── alex.txt
└── Alex.txt
0 directories, 2 files
Taking this into consideration, I would try to double check that the paths you are using in your namespace are actually identical to what is found at the file system level. (i.e: AAA directory with only uppercase letters ; Core directory capitalized and Tools.php file capitalized)
If you want to keep your existing file system layout, you can use PSR-4 to explicitly tell Composer how to map the namespace to the file system:
Change autoload section from your composer.json:
{
"autoload": {
"psr-4": {"AAA\\DB\\": "db/"}
}
}
where db/ is the actual path from the file system
Update the autoloader:
$ composer dump-autoload
This will take care of directory names, but doesn't apply for files. Meaning that any file inside db/ must be named exactly as used in namespace (for a usage as use AAA\DB\DBCore the file must be db/DBCore.php).
If your file is named dbcore.php and you want to reference it in your namespace as DBCore, you can use classmap feature from Composer:
"autoload": {
"classmap": [
"db/dbcore.php"
]
}
dbcore.php:
<?php
namespace AAA\DB;
class DBCore
{
}
And you can use it as usual:
<?php
require_once("./vendor/autoload.php");
$dbCore = new \AAA\DB\DBCore();
Firstly I would check the autoloader files composer has generated to make sure the paths are valid on your linux server.
Another simple but common issue is that on windows the folder and file names are not case sensitive however they are on Linux. Double check that the folders and files have the correct case as if not it won't find them to auto load.
Rather than trying to upload via FTP which I think is going to be tricky if not impossible to get right, I would suggest you explore getting composer working on your hosting environment.
Composer is entirely PHP based, so should run anywhere that PHP is running.
If you don't have command line access, you can use something like PHPShell which gives you a PHP based command line on which you can then run Composer.
See this other SO answer to get some tips on how to use PHPShell.
Another option is to build a little PHP wrapper that you actually run by visiting it in your browser, in the classic PHP way. See this other SO answer for some tips on how to do that.
Bottom line, you should really look at getting Composer running on your server rather than trying to bodge it another way.
Once you have done your composer process on the server, you must remove the PHPShell or composer wrapper you created so that you don't leave any security holes.
Did you tell the composer where your Class 'AAA\Core\Tools' is?
You can even add your own code to the autoloader by adding an autoload field > to composer.json.
{
"autoload": {
"psr-4": {"Acme\\": "src/"}
}
}
Composer is not meant to be used that way (i.e. you shouldn't be manually transferring vendor directories from one environment to another).
As you add dependencies to your project, the composer.json file will contain those dependencies. When you run composer install or update on your localhost, it "locks" the current version of those dependencies for your project and stores them in the composer.lock file. You should be transferring the composer.json and composer.lock files from your development environment to your production environment and then running composer install on your production environment as part of your deployment process. When you run composer install on your production environment, Composer will look at your composer.lock file and install the specified versions of the dependencies in the vendor directory of the production environment.
You may want to review the composer documentation regarding basic usage (https://getcomposer.org/doc/01-basic-usage.md) and command line options (https://getcomposer.org/doc/03-cli.md) for more details.
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!