无法设置$ _SESSION [' lang']

时间:2018-04-27 16:33:10

标签: slim-3

我面对一个坚果要破解在这里...希望那里的人可以帮助我! : - )

我正在制作一个名为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');

最后,这里是错误堆栈跟踪 stack trace of the error

2 个答案:

答案 0 :(得分:0)

我对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'));
    }
}

答案 1 :(得分:0)

经过一点点的考试和尝试/纠正之后,最终解决了这个问题! :

string UDOFileName = System.AppDomain.CurrentDomain.BaseDirectory + "UDOs.xlsx";

if (System.IO.File.Exists(UDOFileName))
{
    // Do some Work
}
else
{
    MessageBox.Show("File not found");
}