Laravel 5 sending information using get request - laravel

In my project I use www.example.com/test?prevUrl=test/3/home this is working fine in development when inside my controller I do $request->prevUrl but doesn't work in production(returns null). can someone help?
edit= "The query_string is not being properly set i-e (?prevUrl=something ) and for that reason my paginator is also not working properly

You can do like it...
In web.php:
Route::get('test','YourController#yourMethod');
In Controller:
public function yourMethod(){
//or return $request->query('prevUrl');
return $request->input('prevUrl');
}

I am using ubuntu 16.04 nginx server. The problem was that $_GET session variables were not being set properly. so what I did was inside /etc/nginx/sites-available/default and inside location / block/ directive I changed try_files $uri $uri/ /index.php?$query_string;to try_files $uri $uri/ /index.php?$args;and it worked.

Related

Images stored in laravel storage/app/protected/images are displayed but return 404

The application stores images in various directories inside storage/app/protected/images directory on Laravel 7. I have had no problem displaying images retrieved from these directories on my localhost, but when I moved to the staging, I have noticed that each request returns 404 even though the image is displayed in the browser.
I have checked the backend and have the same issue with all images in the backend (admin).
the image URL structure is simple: example.com/images/slider/image-name.png
Here is my ImageViewController:
namespace App\Http\Controllers\Client;
use App\Exceptions\ErrorPageException;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;
class ImageViewController extends Controller
{
public function getMainImage($name)
{
if(file_exists(storage_path('app/protected').'/images/'.$name)){
$image = storage_path('app/protected').'/images/'.$name;
}else{
throw new ErrorPageException(404);
}
return response()->file($image);
}
public function getImage($type, $name)
{
if(file_exists(storage_path('app/protected').'/images/'.$type.'/'.$name)){
$image = storage_path('app/protected').'/images/'.$type.'/'.$name;
}else{
throw new ErrorPageException(404);
}
return response()->file($image);
}
}
And here my route:
Route::get('images/{name}', 'Client\ImageViewController#getMainImage');
Route::get('images/{type}/{name}', 'Client\ImageViewController#getImage');
I cannot seem to figure out the problem on my own.
Help please!
Solved!
The problem appeared after including NGINX static file cache directives.
I have changed the approach and all works just fine now!
Here they are for anyone in my situation:
# browser caching of static assets
location ~ /\.(jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
expires 30d;
add_header Pragma public;
add_header Cache-Control "public";
}
# CSS and Javascript
location ~* \.(?:css|js)$ {
expires 1y;
access_log off;
add_header Cache-Control "public";
}
All of this code goes inside the server { ... }

Dingo API routing does not pass in GET parameters when routing to controller

I have a Laravel Application and it uses Dingo Router:
$api->get('/cash-flow', 'App\Http\Controllers\ReportController#cashFlowReport');
When my front-end calls this api, it gets 200 responses from OPTIONS & GET. However, it does not successfully pass in the GET variables.
public function cashFlowReport(Request $request)
{
$input = $request->all();
return var_dump($input);
}
The response returns an empty array. I thought it was the Request class dependency but I think it would throw an error when it tries to access the parameter.
I have the Request Dependency:
use Illuminate\Http\Request;
If you are using nginx you will want to make sure the query string is actually making it through the 'rewrite'/'pretty url' process.
From the Laravel installation documentation for Pretty Urls:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
The ?$query_string part is important.
Laravel Docs - Installation - Pretty Urls

use many ssi with fastcgi_cache in nginx

I am using fastcgi_cache in nginx in order to speed up the php, which connects database and select articles.
Here is my flow:
first, I add a ssi-include in my index:
<\!--# include file="/templates/1-5-list.html" -->
then, I add a location route to handle html -> php in nginx-conf
location ~(\d*?)-(\d*?)-list.html
{
try_files $uri /articles/list.php?p1=$1&p2=$2;
}
after that, I apply fastcgi_cache for list.php
# outside the server{}
fastcgi_cache_path /home/cache/articles levels=1 keys_zone=articles_cache:10m max_size=1024m inactive=1h;
fastcgi_cache_key $scheme$host$request_uri$request_method;
# outside the server{}
location ~/list.php$ {
fastcgi_cache articles_cache;
fastcgi_cache_valid 200 2h;
...
}
Everything is ok right now, and the caching function well.
However, If I have two or more ssi in my index:
<\!--# include file="/templates/1-5-list.html" -->
<\!--# include file="/templates/2-5-list.html" -->
The second ssi return exactly the same result as the first one, FAIL!
I search inside the cache directory, And I found that, the KEY using for caching is httplocalhost/articlesGET, which means such two ssi are sharing the same KEY. And I think this is the cause.
My question is how can I modify the fastcgi_cache_key such that they can have different KEY? I've tried adding fastcgi_cache_key inside location{} but fail.
$request_uri in nginx SSI subrequest refers to parent request URI.
Use $uri in included fragment cache key instead:
fastcgi_cache_key $scheme$host$uri$request_method;

how to process dynamic urls as static pages using nginx?

I want my Nginx serve dynamical urls as static pages, e.g.
given a url "/book?name=ruby_lang&published_at=2014" ,
the nginx will serve a static file (which is generated automatically ) named as:
"book?name=ruby_lang&published_at=2014.html" or:
"book-name-eq-ruby_lang-pblished_at-eq-2014.html"
is this possible?
NOTE:
1.there's no static file named:
"book?name=ruby_lang&published_at=2014.html" nor
"book-name-eq-ruby_lang-pblished_at-eq-2014.html"
however, I can generate them if needed.
2.I can't change the url that give to the consumer. e.g. my consumer could only send request to me via
"/book?name=ruby_lang&published_at=2014"
but not with any other urls.
If you are OK with generating the HTML files yourself, you could simply use nginx's rewrite module. Example:
rewrite ^/book book-name-eq-$arg_name-published_at-eq-$arg_published_at.html last;
If you need to make sure that name and published_at are valid, you can instead do something like this:
location = /book {
if ($arg_name !~ "^[A-Za-z\d_-]+$") { return 404; }
if ($arg_published_at !~ "^\d{4}$") { return 404; }
rewrite ^/book book-name-eq-$arg_name-published_at-eq-$arg_published_at.html last;
}
This will make sure that published_at is a valid 4-digits integer, and name is a valid identifier (English alphabets, numbers, underscore, and hyphen).
To make sure a book is only accessible from one URL, you should throw 404 if the URL is the HTML file. Add this before the previous rule:
location ~ /book-(.*).html {
return 404;
}
OK, thanks to #Alon Gubkin's help, finally I solved this problem, (see : http://siwei.me/blog/posts/nginx-try-files-and-rewrite-tips) .here are some tips:
use 'try_files' instead of 'rewrite'
use '-' instead of underscore '_' in your static file names, otherwise nginx would get confused when setting $arg_parameters to your file name. e.g. use "platform-$arg_platform.json' instead of "platform_$arg_platform.json"
take a look at nginx built-in variables.
and this is my nginx config snippet:
server {
listen 100;
charset utf-8;
root /workspace/test_static_files;
index index.html index.htm;
# nginx will first search '/platform-$arg_platform...' file,
# if not found return /defautl.json
location /popup_pages {
try_files /platform-$arg_platform-product-$arg_product.json /default.json;
}
}
also I put my code on github so that someone interested in this issue could take a look:
https://github.com/sg552/server_dynamic_urls_as_static_files

Redirect to other domain if static files do not exist

I would like to configure nginx so that
The requested path is a static file, serve it;
otherwise redirect an external domain: http://example.net/.
I do understand that I need to use the try_files directive, but I do not understand how to implement the fallback mechanism.
Found after many tests. What happens if a static file is not found is defined in the last parameter of try_files. You can set that parameter to an internal location. In my case I used
server {
...
root /srv/example.org/web/static;
index index.html;
try_files $uri #redirect-to-dot-net;
location #redirect-to-dot-net {
rewrite ^ http://example.net redirect;
}
}

Resources