Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lumen simple route request doesn't work

Tags:

laravel

lumen

I installed Lumen on my web server, but I have problems with routes

// http://12.345.678.910/
$app->get('/', function() use ($app) {
    return "This works";
});

But in this second case he cant find directory

// http://12.345.678.910/api
$app->get('/api', function() use ($app) {
    return "This dont work";
});

In second case I am getting standard 404 error.

The requested URL /api was not found on this server.

I use Apache, Ubuntu, PHP 5.5 and Lumen

like image 425
Ivan Vulović Avatar asked Aug 28 '15 23:08

Ivan Vulović


3 Answers

It sounds like your URL rewriting isn't working. If you add index.php to the URL right before the /api does it work?

For example, yourdomain.com/api would become yourdomain.com/index.php/api and if the second URL works, then rewriting isn't working.

If your rewriting isn't working, but you have the .htaccess file in your public directory, then you probably need to allow overrides in your Apache configuration. Here is an example virtual host configuration for Lumen on Ubuntu.

I've marked the lines you need to change. Change the first and third to point to the public directory in your website's directory. Then change the second line to the domain name you're using with your website.

<VirtualHost *:80>
    DocumentRoot "/var/www/lumen/public"      # Change this line
    ServerName yourdomain.com                 # Change this line
    <Directory "/var/www/lumen/public">       # Change this line
        AllowOverride All    # This line enables .htaccess files
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

You'll need to restart Apache for these settings to take effect.

A Better Way

Enabling the .htaccess file should work, but using .htaccess slows down your site some. The best solution is to put the contents of the .htaccess file in your virtual host, and then disable .htaccess files.

The example virtual host configuration for that looks like this:

<VirtualHost *:80>
    DocumentRoot "/var/www/lumen/public"  # Change this line
    ServerName yourdomain.com             # Change this line

    <Directory "/var/www/lumen/public">   # Change this line
        # Ignore the .htaccess file in this directory
        AllowOverride None

        # Make pretty URLs
        <IfModule mod_rewrite.c>
            <IfModule mod_negotiation.c>
                Options -MultiViews
            </IfModule>

            RewriteEngine On

            # Redirect Trailing Slashes...
            RewriteRule ^(.*)/$ /$1 [L,R=301]

            # Handle Front Controller...
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^ index.php [L]
        </IfModule>
    </Directory>
</VirtualHost>

Once again, you'll need to restart Apache for these settings to take effect.

like image 66
BrokenBinary Avatar answered Nov 12 '22 01:11

BrokenBinary


Probably the .htaccess file under public folder is missing. Check this: https://github.com/laravel/lumen/blob/master/public/.htaccess

like image 39
llioor Avatar answered Nov 12 '22 01:11

llioor


In the root of your application, create a .htaccess file if its not already there. Then paste the following code:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

I assuming that you are using apache server and have mod_rewrite turned on.

Read basic configuration section in Lumen documentation.

If you are unsure how to turn on mod_rewrite, this stackoverflow post might help you.

like image 2
Subash Avatar answered Nov 11 '22 23:11

Subash