URL problem with Toolbar /

Asked

Viewed 1,452 times

1

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

However, I have a problem that so far has not mattered. And now I want to know why.

I have a simple route to all my pages:

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

Then in the Frontendcontroller.php I have the call from my pages:

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

So if I type:

http://localhost/project/products
http://www.dominio.com.br/produtos

Enters the page normally.

But if I type:

http://localhost/project/products/
http://www.dominio.com.br/produtos/

He doesn’t go in and redirect to:

http://localhost/products
NOT FOUND

The problem is when you put that / 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');
}

3 answers

1


Instead of:

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

Do this:

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

In the documentation there is no Slash indication inside the parameter in the prefix (at the beginning of the string), so if it is root it must be empty.

Also note that you cannot use capital letters in the methods, except in the first letter, that is to say change this (because in Laravel it is all case-sensitive):

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

For this reason:

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

I tested in the Laravel5 and worked so much with Slash / as without, if the problem still exists it may be that you are not using the folder public as root as you did in the other reply.

The situation of redirecting requests with . htaccess to public it works, but since Laravel wasn’t designed to work like this then maybe this is a side effect.

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

The command line to execute him is this:

php -S localhost:8000 server.php

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

If you are using Windows to facilitate you can create a .bat (and the php path nay is in the environment variable PATH) in your project folder:

@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 finish the server just click Ctrl + C on the open CMD screen (or simply click close).

Also note that simulating 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, so:

  • 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');
    
  • the problem is that the user can still insert the bar directly at the end of the url.

  • @Danielomine As far as I understand it is exactly what the AP wants. Want it to be accessible with bar and without bar at the end.

1

Define the directive Rewritebase

RewriteBase /

Modify the Slash trailing rule:

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

Just removed the bar on /$1 because we have already defined the RewriteBase

Note: Remove a duplicate line

RewriteCond %{REQUEST_FILENAME} !-d

It does not need to have two lines equal to this. Note that it is a mere observation and has no relation to the context of your question.

0

Hey, buddy, send your controller so we can see.

It may be that this occurs, because you have an Overload with and without parameter, then when you use / at the end, he understands that it is the method with parameter.

Give a look if that’s it, whatever, send the controller with the signatures so we can analyze.

  • There’s nothing in the controller, just the functions. I’ll post.

  • if you have the functions, then you have something SIM in the controller, that’s what I asked for :-|

Browser other questions tagged

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