URL Problem with Bar /

1

In Laravel it is very common to use friendly URLs and also very easy to use. The system is ready for this.

However, I have a problem that I did not care about until now. And now I want to know why.

I have a simple route for all my pages:

Route::controller('/', 'FrontendController');

Then in FrontendController.php I have the call of my pages:

public function getProdutos(){ }
public function getServicos(){ }
public function getDownload(){ }

So if I type:

  

link link

Go to the page normally.

But if I type:

  

link link

It does not go in and redirects to:

  

link
  NOT FOUND

The problem is when you put this / at the end. No bar works.

.htaccess

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

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

HomeController.php

# Contato
public function getContato(){
    return view('frontend.contato');
}

# Trabalhe Conosco
public function getTrabalheConosco(){
    return view('frontend.trabalhe');
}

# Serviços
public function getServicos(){
    return view('frontend.servicos');
}

# Quem Somos
public function getQuemSomos(){
    return view('frontend.agencia');
}
    
asked by anonymous 11.11.2015 / 19:21

3 answers

1

Instead of:

Route::controller('/', 'FrontendController');

Do this:

Route::controller('', 'FrontendController');

In the documentation there is no indication of the slash within the parameter in the prefix (at the beginning of the string), so if root should be empty.

Also note that you can not use uppercase letters in the methods except for the first letter, or change this (because in Laravel it's all case-sensitive):

  • public function getTrabalheConosco(){ and public function getQuemSomos(){

For this reason:

  • public function getTrabalheconosco(){ and public function getQuemsomos(){

I tested it on Laravel5 and it worked with both slash / and no, if the problem still exists it might be that you are not using the public folder as root as you did in the other answer.

The situation of redirecting requests with .htaccess to public even works, but since Laravel was not a project to run so maybe this is a side effect.

Note that if you are in a development environment you may prefer to use server.php which is a script to be run as stand-alone php server .

The command line to run it is this:

php -S localhost:8000 server.php

The server will be accessible on port 8000, you can easily change it, but note that port 80 may sometimes be in use (as in Apache already pre-installed on some Linux-based or Windows-based distros sometimes by IIS or Skype).

If you are using Windows to make it easier, you can create a .bat (and the php path is not in the environment variable PATH ) in your project:

@echo off
set PHP_BIN="C:\wamp\php\php.exe"
set PHP_INI="C:\wamp\php\php.ini"

%PHP_BIN% -S localhost:8000 -c %PHP_INI% server.php

Run it and to end the server just click on Ctrl + C on the open CMD screen (or simply click close).

Note also that to simulate the test I have two files, one called Controller.php and one called HomeController.php, the second extends from Controller.php and not from BaseController.php, like this:

  • Controller.php

    <?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Foundation\Bus\DispatchesJobs;
    use Illuminate\Routing\Controller as BaseController;
    use Illuminate\Foundation\Validation\ValidatesRequests;
    use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
    
    abstract class Controller extends BaseController
    {
        use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
    }
    
  • HomeController.php

    <?php
    
    namespace App\Http\Controllers;
    
    use App\Http\Controllers\Controller;
    
    class HomeController extends Controller
    {
        # Contato
        public function getContato(){
            return ('frontend.contato');
        }
    
        # Trabalhe Conosco
        public function getTrabalheconosco(){
            return ('frontend.trabalhe');
        }
    
        # Serviços
        public function getServicos(){
            return ('frontend.servicos');
        }
    
        # Quem Somos
        public function getQuemsomos(){
            return ('frontend.agencia');
        }
    }
    
  • routes.php:

    <?php
    
    Route::get('/', function () {
        return view('welcome');
    });
    
    Route::controller('', 'HomeController');
    
27.12.2015 / 20:24
1

Set the RewriteBase

RewriteBase /

Modify the slash trailing rule:

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

Only the bar in /$1 has been removed since we already have defined RewriteBase

Note: Remove a duplicate line

RewriteCond %{REQUEST_FILENAME} !-d

You do not have to have two lines like this. Note that this is a mere observation and has nothing to do with the context of your question.

    
28.12.2015 / 10:50
0

Friend, send your controller to see us.

Take a look if that's it, qlq thing, send the controller with the signatures so we can analyze.

    
11.11.2015 / 19:28