Routes with slim PHP framework

Asked

Viewed 1,700 times

1

Good afternoon, I have almost no experience with slim framework and am giving a studied by book. I downloaded via Composer and installed version 3.8 and had some differences. I tried to create a simple route, but only works when I call the root directory, if I call the route, I have return 404, I put only one 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();
  • Using Apache or php-standalone-server?

  • Use some . htaccess?

  • No, the paperwork says nothing about that.

  • What a mistake just?

  • @Jonathandetoni he already said tenho retorno 404, 404 is a page HTTP status not found.

1 answer

0


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

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

Ready, should work, but using apache 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 must 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 the 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 cannot edit from Virtualhost you can try from . 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 to the address meuprojetoslim.exemplo.com work it is necessary to edit the hosts within the system32 (all this if it is windows), then follow the steps:

  1. disable the Antivirus
  2. Open Notepad.exe as administrator
  3. Press Ctr+O on the Notepad
  4. When the choose file dialog appears type this C:\Windows\System32\drivers\etc\hosts in the field Nome:

This is gonna come up (or good-looking):

# 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 antivirus, restart Apache/Wamp/Xampp, then navigate to the address:

http://meuprojetoslim.exemplo.com

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

While navigating to:

http://meuprojetoslim2.exemplo.com

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

When navigating to any other address like: 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 like this.

  • Thank you for your answer, but this is light years of my knowledge. .kkk.. I put a simple . htacess at the root and it worked. When you download the slim in older versions 2.. , this file comes by default, but in new no, you can tell me why?

  • @Ronaldolopes do not know, probably you must have got confused in something, or forgot to activate or move something.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.