Haven't test with Laravel 5, but L4 has no problem with trailing slashes in Url.
L5 : Pagination url
Hello,
When I test pagination on a fresh Laravel 5 with a simple code :
$user = \App\User::paginate(2);
dd($user->render());
I get this sort of url :
...public/test/?page=2
There is a slash "/" before the "?" so the url is not correct.
Do I miss something ?
Hi, this is the correct behaviour. You can test it yourself by adding the following routes in your routes.php file:
$router->get('/abc',function(){return 'hi';});
$router->get('abc/',function(){return 'hi again';});
$router->get('/abc/',function(){return 'helooo :(';});
Now run the ./artisan route:list command and you will see only one entry for the abc route.
tl;dr
'test?page=1` and 'test/?page=1' are the same.
Hello usman
I know all these syntaxes produce the same route, and you forgot :
$router->get('abc',function(){return 'another hi';});
But when you say that
'test?page=1` and 'test/?page=1' are the same
I am confused because with 'test/?page=1' I get "The requested URL /test was not found on this server."
And with 'test?page=1' all is ok.
When I look the requests I have 2 requests sent by the browser :
- .../public/test/?page=1 that return 301 Moved Permanently
- .../test?page=1 that return 404 Not Found
Your document root should be sent to the public directory, not laravel's base directory. The reason it's resulting in a 404 isn't the trailing slash, it's because your server is looking for a test folder outside of Laravel's public directory.
Hello thepsion5
When I send the url :
.../public/test/?page=1
the browser gets the response 301(because of the trailing slash) and sends the other request, it's not me ^^
I'd like to have :
.../public/test?page=1
generated by Laravel... It's ok with Laravel 4 but not with Laravel 5...
I am in localhost and dont use mod_rewrite so I keep "public" in the url...
I also get
/fragment/fragment/?page=2
and a 301 in Laravel 5. It's still in progress...
@bestmomo if you really want to have:
.../public/test?page=1
Then remove the '/' in the constructor of paginator class i.e.
//change this line
$this->path = $this->path != '/' ? rtrim($this->path, '/').'/' : $this->path;
//to
$this->path = $this->path != '/' ? rtrim($this->path, '/') : $this->path;
PS: It's not a good idea to edit core files.
IIRC, Laravel will always generate its links based on the root directory of the URL, which is why you're supposed to set the public folder as your document root. You'll be dealing with issues like this a lot.
IIRC, Laravel will always generate its links based on the root directory of the URL, which is why you're supposed to set the public folder as your document root. You'll be dealing with issues like this a lot.
+1
@usman I have changed the core to continue to play with pagination.
@thepsion5 I always keep public folder in url when i do local test and I never got issues with version 4. With V 5 the only issue is with pagination, and this is not this public folder that is the problem as is said by tios.
So the good way is to wait for a more stable version and just adapt the core to play correctely.
Hello, I also encountered the same error. I should like to fix this right.
Please! I try to follow the way of @usman but failed, it does not change anything.
@zaknight, in stead of running a local server use Laravel Homestead, and you won't run into these kind of issues (and even more important) the server setup is optimized for development of Laravel projects.
Dear @MThomas, I am currently running applications on the hosting service.
Running laravel on shared hosting is always been tricky. A solution like DigitalOcean might be an option?
@MThomas on Windows it's not so easy to get these tools running, there is some other posts on this point.
@bestmomo I know as I am on Windows myself, but it is doable, you just have to try what to do in CMD and what to do on the virtual machine. But yeah it is not easy :(
http://demo.zatoday.com/public/category/menu-1 This is my problem when using pagination
@ZaKnight This is why it's dangerous to not setup your document root properly.
Simple viewing of blade files http://demo.zatoday.com/resources/views/layouts/app.blade.php
Or if you're lucky enough to find database passwords in logs http://demo.zatoday.com/storage/logs/laravel-2014-12-06.log
And yes, you even have your MySQL server open for % (any IP)
Thanks @bashy, I'm really bad when your application security, but the problem now is how to pagionation my work properly. Please help me.
It will work properly when you have set the document root to point directly to
/home/zaknight/public_html/demo/public
Thank you much @bashy, it worked. really is due to the language problem that I have difficulty in accessing technology, as well as communicate with people. If there is a mistake for people to ignore. Once again I would like to thank, the enthusiastic help of the people, it makes me feel happy.
No problem. Just be careful of allowing people to view files they shouldn't be able to!
Are you using the built in php server ?
@MThomas I have a similar problem as the one in question, but it does open the page though. The problem is, it cannot find the css file or images because apparently the trailing slash works as a further folder.. This happens in DigitalOcean, which is set up with Forge. In my local environment everything works perfect!
Did anyone ever solve this issue?
My document route IS set to public, but Laravel 5.1 is returning pagination urls with an end slash before the argument. Like this:
/example/?page=2
instead of
/example?page=2
And since I'm calling the link via ajax, everything errors because the ajax hits a redirect upon trying to go to "/example/?page=2".
try this: $user = \App\User::paginate(2);
$user->setPath('');
dd($user->render());
If you are developing in an environment with subfolders, try this tweak to the .htaccess:
Replace this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
with this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s/+(.+?)/+[?\s]
RewriteRule /$ /%1 [L,R=301,NE]
@shimohira - Thanks a lot!! It worked!! I spent a half day for that issue and finally a single line helped me...!
At last Laravel 5.2 make me happy with this old problem :)
Please or to participate in this conversation.