无法设置 $_SESSION['lang']
Having trouble to set $_SESSION['lang']
我在这里遇到了一个难题...希望有人能帮助我!? :-)
我正在用 slim3 制作一个网站,我试图在 CodeCourse 上根据这门课程设置本地化,我似乎无法正确设置我的会话,因为我使用的是 DI-Bridge,而他不是...
我似乎无法弄清楚,为什么它根据堆栈跟踪失败了!?
有人可以帮忙吗?
这是我的 contaner.php
use function DI\get;
use Interop\Container\ContainerInterface;
use Slim\Csrf\Guard;
use Slim\Flash\Messages;
use Slim\Views\Twig;
use Slim\Views\TwigExtension;
use Illuminate\Translation\Translator;
use Illuminate\Translation\FileLoader;
use Illuminate\Filesystem\Filesystem;
//use Noodlehaus\Config;
use app\views\extensions\TranslationExtension;
use app\handlers\auth\Auth;
use app\handlers\errors\NotFoundHandler;
use app\models\data\About;
use app\models\data\Contact;
use app\models\data\Framework;
use app\models\data\Project;
use app\models\data\Service;
use app\models\data\Site;
use app\models\data\Testimonial;
use app\models\data\User;
use app\validation\contracts\ValidatorInterface;
use app\validation\Validator;
use app\validation\domain\DomainCheck;
use app\validation\email\EmailAvailable;
use app\validation\password\MatchesPassword;
return [
/**
* attaching : XX ->
*/
/* ROUTER */
'router' => get(Slim\Router::class),
/* 404 ERROR */
'notFoundHandler' => function(ContainerInterface $c) {
return new NotFoundHandler ($c->get(Twig::class));
},
/* AUTH */
Auth::class => function (ContainerInterface $c) {
return new Auth;
},
/* CSRF */
Guard::class => function (ContainerInterface $c) {
return new Guard;
},
/* FLASH */
Messages::class => function (ContainerInterface $c) {
return new Messages;
},
/* VALIDATOR */
ValidatorInterface::class => function (ContainerInterface $c) {
return new Validator;
},
/* TRANSLATOR */
Translator::class => function (ContainerInterface $c) {
$fallback = $c->get('settings.translations.fallback');
$loader = new FileLoader(
new Filesystem(), $c->get('settings.translations.path')
);
$translator = new Translator($loader, $_SESSION['lang'] ?? $fallback);
$translator->setFallback($fallback);
return $translator;
},
/* TWIG */
Twig::class => function (ContainerInterface $c) {
$view = new Twig(__DIR__ . '/../resources/views', [
'cache' => false
]);
$view->addExtension(new TwigExtension(
$c->get('router'),
$c->get('request')->getUri()
));
$view->addExtension(new TranslationExtension(
$c->get(Translator::class)
));
$view->getEnvironment()->addGlobal('flash', $c->get(Messages::class));
$view->getEnvironment()->addGlobal('auth', [
'check' => $c->get(Auth::class)->check(),
'user' => $c->get(Auth::class)->user(),
]);
return $view;
},
/**
* attaching : RESPECT -> VALIDATION -> CUSTOM RULES
*/
/* DOMAIN */
DomainCheck::class => function (ContainerInterface $c) {
return new DomainCheck;
},
/* EMAIL */
EmailAvailable::class => function (ContainerInterface $c) {
return new EmailAvailable;
},
/* PASSWORD */
MatchesPassword::class => function (ContainerInterface $c) {
return new MatchesPassword ($c->get(password));
},
/**
* attaching : MODELS ->
*/
/* ABOUT */
About::class => function (ContainerInterface $c) {
return new About;
},
/* CONTACT */
Contact::class => function (ContainerInterface $c) {
return new Contact;
},
/* FRAMEWORK */
Framework::class => function (ContainerInterface $c) {
return new Framework;
},
/* PROJECT */
Project::class => function (ContainerInterface $c) {
return new Project;
},
/* SERVICE */
Service::class => function (ContainerInterface $c) {
return new Service;
},
/* SITE */
Site::class => function (ContainerInterface $c) {
return new Site;
},
/* TESTIMONIAL */
Testimonial::class => function (ContainerInterface $c) {
return new Testimonial;
},
/* USER */
User::class => function (ContainerInterface $c) {
return new User;
},
];
这是我的 TranslationController.php
<?php
namespace App\Controllers;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Router;
class TranslationController {
/**
* @param Request $request
* @param Response $response
* @param Router $router
* @param $args
*
* @return mixed
*/
public function switch(Request $request, Response $response, Router $router, $args) {
if (isset($args['lang'])) {
$_SESSION['lang'] = $args['lang'];
}
return $response->withRedirect($router->pathFor('home'));
}
}
加上翻译路线:
<?php
/**
* adding to view : LOCALIZATION
*/
$app->get('/translate/{lang}', ['app\controllers\TranslationController', 'switch'])->setName('translate.switch');
最后,这是错误堆栈跟踪
我对 PHP DI 没有太多经验,但我建议删除 $args
参数并通过 $request->getAttribute('lang')
获取参数。
<?php
namespace App\Controllers;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Router;
class TranslationController {
/**
* @param Request $request
* @param Response $response
* @param Router $router
* @param $args
*
* @return mixed
*/
public function switch(Request $request, Response $response, Router $router) {
$_SESSION['lang'] = $request->getAttribute('lang');
return $response->withRedirect($router->pathFor('home'));
}
}
经过更多检查和 try/correct,最终解决了这个问题! :
public function switch(Request $request, Response $response, Router $router, $lang) {
if (isset($lang)) {
$_SESSION['lang'] = $lang;
}
return $response->withRedirect($router->pathFor('home'));
}
我在这里遇到了一个难题...希望有人能帮助我!? :-)
我正在用 slim3 制作一个网站,我试图在 CodeCourse 上根据这门课程设置本地化,我似乎无法正确设置我的会话,因为我使用的是 DI-Bridge,而他不是...
我似乎无法弄清楚,为什么它根据堆栈跟踪失败了!?
有人可以帮忙吗?
这是我的 contaner.php
use function DI\get;
use Interop\Container\ContainerInterface;
use Slim\Csrf\Guard;
use Slim\Flash\Messages;
use Slim\Views\Twig;
use Slim\Views\TwigExtension;
use Illuminate\Translation\Translator;
use Illuminate\Translation\FileLoader;
use Illuminate\Filesystem\Filesystem;
//use Noodlehaus\Config;
use app\views\extensions\TranslationExtension;
use app\handlers\auth\Auth;
use app\handlers\errors\NotFoundHandler;
use app\models\data\About;
use app\models\data\Contact;
use app\models\data\Framework;
use app\models\data\Project;
use app\models\data\Service;
use app\models\data\Site;
use app\models\data\Testimonial;
use app\models\data\User;
use app\validation\contracts\ValidatorInterface;
use app\validation\Validator;
use app\validation\domain\DomainCheck;
use app\validation\email\EmailAvailable;
use app\validation\password\MatchesPassword;
return [
/**
* attaching : XX ->
*/
/* ROUTER */
'router' => get(Slim\Router::class),
/* 404 ERROR */
'notFoundHandler' => function(ContainerInterface $c) {
return new NotFoundHandler ($c->get(Twig::class));
},
/* AUTH */
Auth::class => function (ContainerInterface $c) {
return new Auth;
},
/* CSRF */
Guard::class => function (ContainerInterface $c) {
return new Guard;
},
/* FLASH */
Messages::class => function (ContainerInterface $c) {
return new Messages;
},
/* VALIDATOR */
ValidatorInterface::class => function (ContainerInterface $c) {
return new Validator;
},
/* TRANSLATOR */
Translator::class => function (ContainerInterface $c) {
$fallback = $c->get('settings.translations.fallback');
$loader = new FileLoader(
new Filesystem(), $c->get('settings.translations.path')
);
$translator = new Translator($loader, $_SESSION['lang'] ?? $fallback);
$translator->setFallback($fallback);
return $translator;
},
/* TWIG */
Twig::class => function (ContainerInterface $c) {
$view = new Twig(__DIR__ . '/../resources/views', [
'cache' => false
]);
$view->addExtension(new TwigExtension(
$c->get('router'),
$c->get('request')->getUri()
));
$view->addExtension(new TranslationExtension(
$c->get(Translator::class)
));
$view->getEnvironment()->addGlobal('flash', $c->get(Messages::class));
$view->getEnvironment()->addGlobal('auth', [
'check' => $c->get(Auth::class)->check(),
'user' => $c->get(Auth::class)->user(),
]);
return $view;
},
/**
* attaching : RESPECT -> VALIDATION -> CUSTOM RULES
*/
/* DOMAIN */
DomainCheck::class => function (ContainerInterface $c) {
return new DomainCheck;
},
/* EMAIL */
EmailAvailable::class => function (ContainerInterface $c) {
return new EmailAvailable;
},
/* PASSWORD */
MatchesPassword::class => function (ContainerInterface $c) {
return new MatchesPassword ($c->get(password));
},
/**
* attaching : MODELS ->
*/
/* ABOUT */
About::class => function (ContainerInterface $c) {
return new About;
},
/* CONTACT */
Contact::class => function (ContainerInterface $c) {
return new Contact;
},
/* FRAMEWORK */
Framework::class => function (ContainerInterface $c) {
return new Framework;
},
/* PROJECT */
Project::class => function (ContainerInterface $c) {
return new Project;
},
/* SERVICE */
Service::class => function (ContainerInterface $c) {
return new Service;
},
/* SITE */
Site::class => function (ContainerInterface $c) {
return new Site;
},
/* TESTIMONIAL */
Testimonial::class => function (ContainerInterface $c) {
return new Testimonial;
},
/* USER */
User::class => function (ContainerInterface $c) {
return new User;
},
];
这是我的 TranslationController.php
<?php
namespace App\Controllers;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Router;
class TranslationController {
/**
* @param Request $request
* @param Response $response
* @param Router $router
* @param $args
*
* @return mixed
*/
public function switch(Request $request, Response $response, Router $router, $args) {
if (isset($args['lang'])) {
$_SESSION['lang'] = $args['lang'];
}
return $response->withRedirect($router->pathFor('home'));
}
}
加上翻译路线:
<?php
/**
* adding to view : LOCALIZATION
*/
$app->get('/translate/{lang}', ['app\controllers\TranslationController', 'switch'])->setName('translate.switch');
最后,这是错误堆栈跟踪
我对 PHP DI 没有太多经验,但我建议删除 $args
参数并通过 $request->getAttribute('lang')
获取参数。
<?php
namespace App\Controllers;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Router;
class TranslationController {
/**
* @param Request $request
* @param Response $response
* @param Router $router
* @param $args
*
* @return mixed
*/
public function switch(Request $request, Response $response, Router $router) {
$_SESSION['lang'] = $request->getAttribute('lang');
return $response->withRedirect($router->pathFor('home'));
}
}
经过更多检查和 try/correct,最终解决了这个问题! :
public function switch(Request $request, Response $response, Router $router, $lang) {
if (isset($lang)) {
$_SESSION['lang'] = $lang;
}
return $response->withRedirect($router->pathFor('home'));
}