codeigniter 4 url 进入循环
codeigniter 4 url goes into loop
我认为问题可能来自 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
我很确定你需要更换 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));
}
}
如果我遗漏了什么,请告诉我。
我认为问题可能来自 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
我很确定你需要更换 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));
}
}
如果我遗漏了什么,请告诉我。