Routes with slim PHP framework [duplicate]

1

Good afternoon, I have almost no experience with slim framework and I am giving a study by bookSlim . I downloaded it via composer and installed version 3.8 and it had some differences. I tried to create a simple route, but it only works when I call the root directory, if I call the route, I have 404 return, I just put an echo to see if it is working.

<?php
    require 'vendor/autoload.php';
    use \Slim\App;
    $app = new App();
    $app->get('/', function () {
        echo "index";
    });
    $app->get('/ola', function(){
        echo "teste com rota";
    });
    $app->run();
    
asked by anonymous 06.10.2017 / 19:32

1 answer

0

If you are using PHP Built-in web server > simply navigate to the public folder by CMD or Terminal (depending on your operating system), assuming it is in the Documents folder (on windows), do this:

cd C:\Users\[nome do seu usuário]\Documents\projeto-slim\public
php -S localhost:8080 index.php

It should work, but using apache you will have to navigate to public, assuming your folder is c:\xampp\htdocs\projeto-slim or c:\wamp\htdocs\projeto-slim or c:\apache\htdocs\projeto-slim , you should access via browser something like:

http://localhost/projeto-slim/public/

Of course, to remove the public from the URL you can use a .htaccess or virtualHost,

VirtualHost

To use VirtualHost edit vhost.conf or httpd.conf, it should look something like this:

#Para acessar tudo que não use frameworks
<VirtualHost *:80>
    DocumentRoot "c:\apache\htdocs\"
    ....
</VirtualHost>

# para acessar seu projeto
<VirtualHost *:80>
    DocumentRoot "c:\apache\htdocs\projeto-slim\public\"
    ServerName meuprojetoslim.exemplo.com
    ...
</VirtualHost>

# para acessar seu segundo projeto
<VirtualHost *:80>
    DocumentRoot "c:\apache\htdocs\projeto-slim2\public\"
    ServerName meuprojetoslim2.exemplo.com
    ...
</VirtualHost>

htaccess

If you can not edit through VirtualHost you can try for .htaccess, create a .htaccess in the root folder of your site, with something like:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^meuprojetoslim\.exemplo\.com$ [OR] # sem www
RewriteCond %{HTTP_HOST} ^www\.meuprojetoslim\.exemplo\.com$ # com www
RewriteRule ^ projeto-slim2/public/ [L]                        # o L é pra impedir conflitar com as próximas regras

RewriteCond %{HTTP_HOST} ^meuprojetoslim2\.exemplo\.com$ [OR] # sem www
RewriteCond %{HTTP_HOST} ^www\.meuprojetoslim2\.exemplo\.com$ # com www
RewriteRule ^ projeto-slim2/public/ [L]                       # o L é pra impedir conflitar com as próximas regras

Of course, for the address meuprojetoslim.exemplo.com it is necessary to edit the hosts within system32 (this is everything for windows), then follow the steps:

  • disable antivirus
  • Open notepad.exe as administrator
  • Press Ctr + O on notepad
  • When the file choose window appears, type C:\Windows\System32\drivers\etc\hosts in the Nome:
  • This will appear (or very similar):

    # localhost name resolution is handled within dns itself.
        127.0.0.1       localhost
        ::1             localhost
    

    Add this:

    # localhost name resolution is handled within dns itself.
        127.0.0.1       localhost
        ::1             localhost
        127.0.0.1       meuprojetoslim.exemplo.com
        127.0.0.1       www.meuprojetoslim.exemplo.com
        127.0.0.1       meuprojetoslim2.exemplo.com
        127.0.0.1       www.meuprojetoslim2.exemplo.com
    

    Save the document, reactivate the virus scanner, restart Apache / Wamp / Xampp and then navigate to:

    http://meuprojetoslim.exemplo.com
    

    Will access c:\apache\htdocs\projeto-slim\public\

    When navigating to:

    http://meuprojetoslim2.exemplo.com
    

    Will access c:\apache\htdocs\projeto-slim\public\

    When navigating to any other address such as http://localhost or http://127.0.0.1

    Will access c:\apache\htdocs\

    You can create a VirtualHost (or a RewriteCode + RewriteRule ) for each project as well.

        
    06.10.2017 / 19:49