PatrickLouys / no-framework-tutorial

A small tutorial to show how to create a PHP application without a framework.
MIT License
1.53k stars 186 forks source link

Couldn't get Step 06 to work #70

Open zethon opened 4 years ago

zethon commented 4 years ago

Struggled with Part 6 for a while because it kept telling the class Example\Controllers\Homepage could not be found. Took me about two hours to figure out I needed to run composer update.

This is not immediately obvious and should be mentioned in the tutorial.

ivanfranco502 commented 4 years ago

Thanks! I was struggling with the same issue, luckily I saw your issue. After a while, I realized my problem was the working directory where I was running the server. You should use public directory to run php -S localhost:8000 instead of root directory.

Wondarar commented 2 years ago

At step6, when running running the site on root, e.g my example is on testsite2.loc, I get: Imgur Am I supposed to see it at this step still? I bet not, I don't know to which step go back and debug.

Wondarar commented 2 years ago

an on /public I still get the 404....?

Imgur

ellisgl commented 2 years ago

Need a .htaccess file in your public folder.

Wondarar commented 2 years ago

Sorry I don'T get it. When I put the index.php in the root and echo a simple "hello world" all is fine. Why do I need the htaccess now? And what should I put into htaccess?

SharakPL commented 2 years ago

@Wondarar you need to start your server in the public so either:

php -S localhost:8000 -t public/

from the root of your project or directly from public folder:

cd public
php -S localhost:8000

I see you created virtual host testsite2.loc so make sure it points to public folder, not the root of your project.

.htaccess is not required :)

Wondarar commented 2 years ago

Ok, since not required I skip that. Here's the testsite2.loc.conf file and in yellow what I added today (which is obsolete then anyways).

Imgur

Where do I set the pointer to public? Is it within this conf file? Is it in line 12, adding \public ?

If I understand it correcty, $routeInfo should NOT give me an empty array? But since it keeps being empty it correctly resolves to the 404.

Imgur

And here's the Route.php

Imgur

SharakPL commented 2 years ago

Your config is invalid. <Directory> block should be inside <VirtualHost>. Here's an example:

<VirtualHost *:80>
    # first VH is default for apache - do not change it
</VirtualHost>

<VirtualHost *:80>
    ServerName mysite.test
    DocumentRoot "/path/to/mysite/public"
    <Directory  "/path/to/mysite/public">
        Options +Indexes +Includes +FollowSymLinks +MultiViews
        AllowOverride All
        Require local
    </Directory>
</VirtualHost>

Then make sure /etc/hosts file has valid IP pointer for mysite.test. Here's well written tutorial

Leave this first <VirtualHost> untouched. It's default for the apache. Just add another below.

Wondarar commented 2 years ago

Working now :1st_place_medal: Thank you! :)

Imgur

SharakPL commented 2 years ago

Glad I could help :) Issue resolved so you can close it now.