Jump to content

PHP structure with Composer does not work in production


montilla

Recommended Posts

Good morning, people! I'm having the following problem in my projectx in php:

I created a project with Composer in php where I put php to listen to the public folder from the command prompt. However, when I upload the project to the site or to the xampp htdocs, the result of displaying the index (domain / projectx / public) is white. It does not display anything or any warning. Running with the php server via prompt listening directly to the public folder the project runs normally.

The structure of the project is this (it has an attached image too):

projeto x
    -App
        -Controller (folder with controllers)
        -Model (folder with models)
        -View (folder with views)
        -Connection (DB connection class)
        -Route (Class that manages the routes accessed in the browser and instantiates the necessary controllers)
    -public
        -.htaccess
        -index.php (Application start page that instantiates the Route class to determine the flow of the application)
    -vendor
        -composer (folder with composer files)
        -autoload.php
    composer.json
    composer.lock
    composer.phar

When I include files in the public folder and access directly, the file works normally. That is, the server is listening to all folders.

 

Could you help me with this? If I have to configure something on the server or in my project? I am new to PHP and would greatly appreciate your help.

Right now, I'm grateful!

Application running on the latest version of Composer. Tested on php version 5.6.40 and 7.1

 

 

Screenshot_3.jpg

Link to comment
Share on other sites

3 hours ago, montilla said:

When I include files in the public folder and access directly, the file works normally. That is, the server is listening to all folders.

Does that mean if you access the url http://example.com/public/index.php then things work?

If so then you'll either need to re-configure your server so that the document root points to the public folder or setup a .htaccess file in your project folder to redirect requests to the public folder.

Otherwise, if you mean adding a different .php file to the public folder and it works but the index.php file does not, then as mentioned above you probably have an error.  The best way to find out is to check the error log, either for your server or a separate php error log depending on how it's configured.  Create a file that executes the phpinfo() function to see how things are configured.

 

  • Like 1
Link to comment
Share on other sites

Good night!

The context is that I use a shared hosting, and the folder available to the project is not the root of hosting.
I researched other topics and will try to make some settings in the hosting tomorrow.
I will come back with the results to share with you.
I am from Brazil and our time zones should be different. Sorry for the divergence.

My sincere thanks from now on.

I hope to see you again and continue to help me on this journey.

Big hug!

11 hours ago, taquitosensei said:

A white screen could be a fatal error. I would turn on error reporting 

put 


ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

at the top of your index.php, if it is a fatal error, the error should display instead of the white screen. 

10 hours ago, kicken said:

Does that mean if you access the url http://example.com/public/index.php then things work?

If so then you'll either need to re-configure your server so that the document root points to the public folder or setup a .htaccess file in your project folder to redirect requests to the public folder.

Otherwise, if you mean adding a different .php file to the public folder and it works but the index.php file does not, then as mentioned above you probably have an error.  The best way to find out is to check the error log, either for your server or a separate php error log depending on how it's configured.  Create a file that executes the phpinfo() function to see how things are configured.

 

Link to comment
Share on other sites

On 5/21/2019 at 11:17 AM, taquitosensei said:

A white screen could be a fatal error. I would turn on error reporting 

put 


ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

at the top of your index.php, if it is a fatal error, the error should display instead of the white screen. 

I included the code without code, but the page remains blank with no reference at all.
What do you indicate?
Remembering that my index.php is not at the root of hosting.

Link to comment
Share on other sites

The terminology you are using is a little odd.  

Servers don't "listen" to directories.  Directories are either in web space or not.  It looks to me like the only folder you want to be in "web space" is the public directory.  Is your web root /public or are all the directories beneath the web root?

There are many background questions that might help shed some light including:

  • What web server is this running under?  Apache? Nginx?
  • Some of this info can be obtained from a quick script in the public directory that just contains phpinfo()
  • What is in the composer.json, and in particular, the autoload configuration?
  • What is in the .htaccess?
  • What is in the index.php?  
  • Is this a framework project (CakePHP maybe?)

When you say that this is shared hosting, does that mean that you don't have a shell and just ftp'd the site from your development box?  Are you able to run composer install on the host?

At the end of the day, there has to be something in a log that tells you what the error is.  Where are the error logs for the site?

 

Link to comment
Share on other sites

On 5/24/2019 at 1:31 AM, gizmola said:

The terminology you are using is a little odd.  

Servers don't "listen" to directories.  Directories are either in web space or not.  It looks to me like the only folder you want to be in "web space" is the public directory.  Is your web root /public or are all the directories beneath the web root?

There are many background questions that might help shed some light including:

  • What web server is this running under?  Apache? Nginx?
  • Some of this info can be obtained from a quick script in the public directory that just contains phpinfo()
  • What is in the composer.json, and in particular, the autoload configuration?
  • What is in the .htaccess?
  • What is in the index.php?  
  • Is this a framework project (CakePHP maybe?)

When you say that this is shared hosting, does that mean that you don't have a shell and just ftp'd the site from your development box?  Are you able to run composer install on the host?

At the end of the day, there has to be something in a log that tells you what the error is.  Where are the error logs for the site?

 

Good afternoon!

I got past the blank page. It was a problem installing Composer on the server.
I can access the index, but the server is not recognizing the other directories in my Route.php file. You are returning a 404 error.
Answering your questions:
* Linux Apache CentOS Server 6.9 - Kinghost Hosting;
* My composer.json:
 

{
    "name": "miniframework",
    "require": {
        "php": ">= 5.6"
    },
    "authors": [
        {
            "name": "Fabio Faria",
            "email": "fabio-oliveri@hotmail.com"
        }
    ],
    "autoload": {
        "psr-4": {
            "App\\": "App/",
            "MF\\": "vendor/MF/"
        }
    }
}


* My autoload.php:
 

<?php

// autoload.php @generated by Composer

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInitf924af3bf344aef2d95a140d392154f6::getLoader();


* My Route.php:
 

<?php

//namespace correspondente ao diretório em que o arquivo está contido
namespace App;

use MF\Init\Bootstrap;

class Route extends Bootstrap{

    protected function initRoutes() {

        //configura array de rotas dos controllers
        $routes["home"] = array(
            "route" => "/",
            "controller" => "IndexController",
            "action" => "index" //action que será desparada no IndexController
        );
        
        $routes["autenticar"] = array(
            "route" => "/autenticar",
            "controller" => "AuthController",
            "action" => "autenticar" //action que será desparada no AuthController
        );
        
        $routes["laudos"] = array(
            "route" => "/laudos",
            "controller" => "AppController",
            "action" => "laudos" //action que será desparada no AppController
        );
        
        $routes["sair"] = array(
            "route" => "/sair",
            "controller" => "AuthController",
            "action" => "sair" //action que será desparada no AuthController
        );
        
        $routes["acao"] = array(
            "route" => "/acao",
            "controller" => "AppController",
            "action" => "acao" //action que será desparada no AppController
        );
       
        $routes["buscar"] = array(
            "route" => "/buscar",
            "controller" => "AppController",
            "action" => "buscar" //action que será desparada no AppController
        );

        $this->setRoutes($routes);
    }
    
}

?>

Right now, I'm grateful!

Link to comment
Share on other sites

On 5/24/2019 at 1:31 AM, gizmola said:

The terminology you are using is a little odd.  

Servers don't "listen" to directories.  Directories are either in web space or not.  It looks to me like the only folder you want to be in "web space" is the public directory.  Is your web root /public or are all the directories beneath the web root?

There are many background questions that might help shed some light including:

  • What web server is this running under?  Apache? Nginx?
  • Some of this info can be obtained from a quick script in the public directory that just contains phpinfo()
  • What is in the composer.json, and in particular, the autoload configuration?
  • What is in the .htaccess?
  • What is in the index.php?  
  • Is this a framework project (CakePHP maybe?)

When you say that this is shared hosting, does that mean that you don't have a shell and just ftp'd the site from your development box?  Are you able to run composer install on the host?

At the end of the day, there has to be something in a log that tells you what the error is.  Where are the error logs for the site?

 

Continuing the answers:
* My .htaccess is empty;
* My index.php:
 

<?php

    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    require_once "../vendor/autoload.php";

    $route = new \App\Route;


?>


* I do not have frameworks, only Composer;
* That. I have only the FTP user with active SSH, which is where I installed Composer.

The homepage of the site is this: http://oficinadoalicatecertific.com.br/
When we click the "Entrar" button at the top right, the action triggers the address /autenticar, but it generates the 404 error.
The home page comes from Route.php, but something does not let you recognize the other routes.

Can you help me?

Link to comment
Share on other sites

16 minutes ago, requinix said:

No, composer is not doing that.

If your .htaccess is empty (or has a Deny from All) then of course /autenticar won't work. You need something in there to route URLs through your PHP script.

But why locally does the project work perfectly?
And if so, what setting should I set in this file?
Whoever takes care of the routes is my Route.php, right?

Link to comment
Share on other sites

18 minutes ago, montilla said:

But why locally does the project work perfectly?

:psychic:

Your local and production environments are not the same. Compare and contrast.

18 minutes ago, montilla said:

Whoever takes care of the routes is my Route.php, right?

Your PHP isn't being executed in the first place. That's the problem. The web server (Apache) has to know that it needs to execute your PHP. If it does not then you'll get those 404s.

Link to comment
Share on other sites

39 minutes ago, requinix said:

:psychic:

Your local and production environments are not the same. Compare and contrast.

Your PHP isn't being executed in the first place. That's the problem. The web server (Apache) has to know that it needs to execute your PHP. If it does not then you'll get those 404s.

How can I fix this?

Link to comment
Share on other sites

I got it! With your tips, I came to the .htaccess issues and studying deeper, I saw that I had to do the routes through it or using a framework that abstracted routes by setting few parameters in .htaccess.
I used the Slim framework.
Thank you all for the promptness!

Strong hug! ???

  • Like 1
Link to comment
Share on other sites

10 hours ago, devank71 said:

Many thanks for sharing such great information about php. Keep sharing.

Glad you worked it out.  We have been doing the same thing for over 15 years now, and hopefully for many more. ?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.