codeigniter 4 url 进入循环
Posted
技术标签:
【中文标题】codeigniter 4 url 进入循环【英文标题】:codeigniter 4 url goes into loop 【发布时间】:2022-01-21 06:48:22 【问题描述】:我认为问题可能来自 LanguageControl。通过一直重复 url 来刷新页面。我在管理面板中使用多语言支持。以下是我认为可能存在问题的所有代码块。
路线
$routes->get('/', 'Frontend\Home::index', ['as' => 'homepage']);
控制器
class Home extends BaseController
public function index()
return sdc_theme_view('index');
sdc_theme_view
function sdc_theme_view($path, $data = [])
$viewPath = THEMES_PATH . sdc_theme_folder();
$renderer = \Config\Services::themeRenderer($viewPath, null, true);
$shortcoder = new \Seiler\Shortcoder\Shortcoder();
$shortcoder->add(shortcodes());
return $shortcoder->parse($renderer->setData($data)->render($path));
语言控制
class LanguageControl implements FilterInterface
public function before(RequestInterface $request, $arguments = null)
$referer = (string) Services::request()->getServer('HTTP_REFERER');
$_SESSION['_ci_previous_url'] = $referer;
$uri = new \CodeIgniter\HTTP\URI(current_url());
$segments = $uri->getSegments();
if (!isset($segments[0]) || !in_array($segments[0], config('app')->supportedLocales))
array_unshift($segments, config('app')->defaultLocale);
$query = $uri->getQuery();
$new_uri = implode('/', $segments);
$new_uri = $query ? $new_uri . '?'. $query : $new_uri;
return redirect()->to(base_url($new_uri));
.env
app.baseURL = 'http://localhost/sdcms/'
enter image description here
【问题讨论】:
【参考方案1】:我很确定您需要替换 array_unshift
。对我来说,当我设置你的代码时,我需要查看 $segments[1] 如果你需要稍微调整一下,请原谅我:
unset($segments[1]);
$segments[] = $defaultLocale;
您的示例的完整代码(但使用 $variables 作为您支持的默认语言环境):
public function before(RequestInterface $request, $arguments = null)
$uri = new \CodeIgniter\HTTP\URI(current_url());
$segments = $uri->getSegments();
$supportedLocales = array('en','fr');
$defaultLocale = 'en';
if (!isset($segments[1]) || !in_array($segments[1],$supportedLocales))
unset($segments[1]);
$segments[] = $defaultLocale;
$query = $uri->getQuery();
$new_uri = implode('/', $segments);
$new_uri = $query ? $new_uri . '?'. $query : $new_uri;
return redirect()->to(base_url($new_uri));
如果我遗漏了什么,请告诉我。
【讨论】:
以上是关于codeigniter 4 url 进入循环的主要内容,如果未能解决你的问题,请参考以下文章
如何从 Codeigniter 4 中的 url 中删除 public/index.php/