如何在流明路由器的每个路由中排除“ / api”(每个正则表达式)?

时间:2019-08-05 19:39:21

标签: regex laravel router lumen lumen-routing

我的流明路由器(web.php)有问题: 我的项目在vue路由器中包含vue.js,因此我想将所有路由都指向路由器,确实可以正常工作。

library(tidyverse)
library(data.table)


# Create vector of real numbers -------------------------------------------------------------------------------------------------
set.seed(2019)

nb <- c(10, 23, 17, 16, 20)
x <- c(
  rnorm(nb[1], mean = 20, sd = 0.5),
  rnorm(nb[2], mean = 5,  sd = 0.1),
  rnorm(nb[3], mean = 10, sd = 0.5),
  rnorm(nb[4], mean = 30, sd = 0.8),
  rnorm(nb[5], mean = 18, sd = 10)
)


# Functions ---------------------------------------------------------------------------------------------------------------------

# Returns all local minima given a density object
find_local_mins <- function(density) {
  y <- density$y
  x <- density$x

  ind_mins <- which(y - shift(y, 1) < 0  & y - shift(y, 1, type = "lead") < 0)
  mins <- x[ind_mins]

  return(mins)
}

# Compute differences between max and min value of a vector between breaks
compute_clusters_ranges <- function(x, breaks) {
  clusters <- cut(x, breaks = c(-Inf, breaks, Inf))
  splits <- split(x, clusters)
  clusters_ranges <- map_dbl(splits, ~ diff(range(.)))

  return(clusters_ranges)
}


# ----------


# Find and plot intervals using gaussian kernel with binwith of 2 ---------------------------------------------------------------
densite <- density(x, kernel = "gaussian", bw = 2, n = 10000) # Estimate density
mins <- find_local_mins(densite) # Find local minima for clustering

plot(densite, xlab = "x", main = "")
rug(x, ticksize = 0.06)
abline(v = mins, col = rep("blue", length(mins)))

# Compute range (difference between max and min value) for each interval --------------------------------------------------------
cluster_ranges <- compute_clusters_ranges(x, mins)
cluster_ranges # Some ranges are still greater than 3, so we cluster again with a smaller binwith


# ----------


# Find and plot intervals using gaussian kernel with binwith of 1 ---------------------------------------------------------------
densite <- density(x, kernel = "gaussian", bw = 1, n = 10000) # Estimate density
mins <- find_local_mins(densite) # Find local minima for clustering

plot(densite, xlab = "x", main = "")
rug(x, ticksize = 0.06)
abline(v = mins, col = rep("blue", length(mins)))

# Compute range (difference between max and min value) for each interval --------------------------------------------------------
cluster_ranges <- compute_clusters_ranges(x, mins)
cluster_ranges # Some ranges are still greater than 3, so we cluster again with a smaller binwith


# ----------


# Find and plot intervals using gaussian kernel with binwith of 0.659 -----------------------------------------------------------
densite <- density(x, kernel = "gaussian", bw = 0.659, n = 10000) # Estimate density
mins <- find_local_mins(densite) # Find local minima for clustering

plot(densite, xlab = "x", main = "")
rug(x, ticksize = 0.06)
abline(v = mins, col = rep("blue", length(mins)))

# Compute range (difference between max and min value) for each interval --------------------------------------------------------
cluster_ranges <- compute_clusters_ranges(x, mins)
cluster_ranges # The empty interval [36.62, 36.63] have been created

```r

I want to obtain natural intervals for a vector of numeric data. In each interval created, I want the difference between the greatest and the smallest value to be less than 3. I want to obtain this using as few intervals as possible.

我的问题是:我也有一些由流明/控制器处理的api路由:

$router->get('{path:.*}', function () {
    return view('app');
});

好吧,路线$router->group(['prefix' => 'api'], function ($router) { $router->group(['prefix' => 'authors'], function ($router) { $router->get('/', 'AuthorController@showAllAuthors'); $router->get('/id/{id}', 'AuthorController@showAuthorById'); }); }); 正常工作。 但是localhost/api/authors返回了应用程序。

我当时正在考虑在Vue路线中添加一个例外:

localhost/api/authors/1

..但这会导致 NotFoundHttpException 。 正则表达式有问题吗? 它应该排除所有以$router->get('{path:^(?!api).*$}' 开头的路由。

1 个答案:

答案 0 :(得分:0)

您真的很亲密。正则表达式发生在laravel路由中的get / post语句之后。像这样:

$router->get('/{catch?}', function () { 
    return view('app'); 
})->where('catch', '^(?!api).*$');

以下是文档供参考: https://laravel.com/docs/5.8/routing#parameters-regular-expression-constraints

编辑:流明特定值应在组前缀中解决。

$router->get('/{route:.*}/', function () { 
    return view('app'); 
});