Backend Setup: Nginx Configuration and Laravel Routing with Custom Pathinfo Parsing
This article demonstrates how to configure Nginx for a Laravel admin backend, define flexible route patterns in routes/admin.php, and implement a BaseController that converts URL path segments into an associative array for easy parameter extraction in controller actions.
The guide starts with an Nginx server block for the admin subdomain admin.test6.local, listening on port 80, setting the document root to d:/data/www/test6/public/admin, and configuring index files, custom error pages, and several location directives to handle PHP execution, deny direct access to uploaded PHP files, and protect hidden files.
#test6 admin
server {
listen 80;
server_name admin.test6.local;
root "d:/data/www/test6/public/admin";
index index.html index.htm index.php;
error_page 404 /404.html;
location = /404.html { return 404 'Sorry, File not Found!'; }
error_page 500 502 503 504 /50x.html;
location = /50x.html { root /usr/share/nginx/html; }
location / { try_files $uri $uri/ /index.php?$query_string; }
location ~ /Uploads/.*\.php$ { deny all; }
location ~ \.php/ { if ($request_uri ~ ^(.+\.php)(/.+?)($|\?)) { } fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param SCRIPT_NAME $1; fastcgi_param PATH_INFO $2; fastcgi_param SCRIPT_FILENAME $document_root$1; }
location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
location ~ /\.ht { deny all; }
}Next, the Laravel routing file routes/admin.php defines three GET routes for the goods list, supporting optional pagination, limit, and keyword parameters:
Route::get('/shop/goods_list', 'Shop\GoodsController@goods_list');
Route::get('/shop/goods_list/page/{page}/limit/{limit}', 'Shop\GoodsController@goods_list');
Route::get('/shop/goods_list/page/{page}/limit/{limit}/keyword/{keyword}', 'Shop\GoodsController@goods_list');The BaseController located at app/Http/Controllers/Admin/Publi/BaseController.php registers a middleware that captures the request path, converts it into an associative array via the path_to_array method, and stores it in $this->pathinfo. The conversion splits the URL by slashes and pairs every even segment with the following odd segment as key‑value pairs.
public function path_to_array(Request $request, $path = null) {
$array = array();
if (strpos($path, '/') !== false) {
$arr = explode('/', $path);
foreach ($arr as $k => $v) {
if ($k > 0 && ($k % 2 == 0)) {
$array[$v] = $arr[$k + 1];
}
}
}
if (count($array) > 0) {
return $array;
}
}Finally, GoodsController extends this base class and uses the parsed $this->pathinfo to retrieve page and keyword parameters, defaulting to 1 and an empty string when they are absent. The controller then dumps the values for debugging.
public function goods_list(Request $request) {
$page = 1;
$keyword = '';
if (is_array($this->pathinfo)) {
if (array_key_exists('page', $this->pathinfo)) {
$page = $this->pathinfo['page'];
}
if (array_key_exists('keyword', $this->pathinfo)) {
$keyword = $this->pathinfo['keyword'];
}
}
dump($page);
dump($keyword);
exit;
}Overall, the article provides a practical example of integrating Nginx server settings with Laravel routing and a custom controller base class to handle clean URL parameters without relying on query strings.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
