# |
Jun 17th 2021, 15:44 |
alamnaryab |
that routes.php is like electric wires for me :,( what should I add at which line of this code https://cakesf.slack.com/archives/C053DPNGT/p1623931730446100 |
# |
Jun 17th 2021, 15:41 |
ndm |
Yeah, the error happens earlier, as suspected, it's missing route error... eg there is no route connected for `/` (without language parameter). |
# |
Jun 17th 2021, 15:40 |
alamnaryab |
RoutingMiddleware is there but mine is not there named: DefaultlngMiddleware |
# |
Jun 17th 2021, 15:37 |
ndm |
You check the stacktrace. If you don't even reach the middleware, then I guess it's a missing route error. |
# |
Jun 17th 2021, 15:36 |
alamnaryab |
How can I check where it originates from I made the param overwritten to english in all cases, it is working because now for `en` and `ar` both is is showing english but this MiddleWare is not being executed when there is not language param |
# |
Jun 17th 2021, 15:33 |
ndm |
Should work fine when placed afterwards with regards to be parameter being added... I'd again press on checking where exactly the error originates from then, it doesn't sound like it stems from a URL array missing the parameter. |
# |
Jun 17th 2021, 15:31 |
alamnaryab |
also tried before RoutingMiddleware even the 1st one |
# |
Jun 17th 2021, 15:29 |
alamnaryab |
as suggested by Mr @ndm after RoutingMiddleWare |
# |
Jun 17th 2021, 15:24 |
ndm |
But yeah... actually, if you put it _before_ the routing middleware, the parameter would get overwritten. |
# |
Jun 17th 2021, 15:24 |
kevin.pfeifer |
i would never |
# |
Jun 17th 2021, 15:24 |
ndm |
Now don't you date to proof my statement wrong :) |
# |
Jun 17th 2021, 15:21 |
kevin.pfeifer |
@alamnaryab have you added your DefaultlngMiddleware before the RoutingMiddleware? |
# |
Jun 17th 2021, 15:18 |
ndm |
It should be. It could theoretically be misplaced order wise, but that should not affect the parameter being added. That error looks like it's not referring to a URL array. Where exactly in the code does it originate from? |
# |
Jun 17th 2021, 15:16 |
alamnaryab |
yes it is `ResponseInterface` but looks like language param is still not added as I am still getting error `A route matching "/" could not be found.` if is not enough that debug() and exit is working in this class for adding maddleware? |
# |
Jun 17th 2021, 15:14 |
ndm |
That snippet there looks like it should generally work. If nothing is being set, then maybe the middleware hasn't been added (correctly). |
# |
Jun 17th 2021, 15:12 |
ndm |
See the return type hint of the method, you must return a response. |
# |
Jun 17th 2021, 15:07 |
alamnaryab |
From `MiddlewareClass` should I return `$request` or `$response` both not adding language parameter, am I doing any mistake also tried commented `return $response` |
# |
Jun 17th 2021, 14:52 |
slackebot |
applies to requests). Trying to get this working is a good exercise, however I'd still suggest that you consider the redirect solution. |
# |
Jun 17th 2021, 14:52 |
ndm |
@alamnaryab You don't need to care about the debug output structure, you use the public API provided by the object. Not sure where you got `addParam()` from, in 4.x you'd use `withParam()` (https://api.cakephp.org/4.2/class-Cake.Http.ServerRequest.html#withParam()), and have to reassign the returned object as it is immutable (https://book.cakephp.org/3/en/controllers/request-response.html#common-mistakes-with-immutable-responses same |
# |
Jun 17th 2021, 14:38 |
alamnaryab |
I have create class as shown in documentation also added in middlewareQueue in application.php how can I modify request params it is protected scoped |
# |
Jun 17th 2021, 14:05 |
admad |
I must them done them on 4.next just to be safe, didn't remember for which branch/release i had done the changes :) |
# |
Jun 17th 2021, 13:59 |
slackebot |
$viewOptions = [] ) { parent::__construct($request, $response, $eventManager, $viewOptions); $this->setSubDir(I18n::getLocale()); } }``` Thanks. |
# |
Jun 17th 2021, 13:59 |
erwane |
Mailer ```public function viewBuilder(): ViewBuilder { $builder = parent::viewBuilder(); $builder->setClassName('Mailer'); return $builder; }``` And View ``` class MailerView extends View { /** * @inheritDoc */ public function __construct( ?ServerRequest $request = null, ?Response $response = null, ?EventManager $eventManager = null, array |
# |
Jun 17th 2021, 13:56 |
dantedantas |
Thank you very much Kevin. I will look and test everything that you sent to me. |
# |
Jun 17th 2021, 13:55 |
ndm |
@alamnaryab You can either create a custom class (https://book.cakephp.org/4/en/controllers/middleware.html#creating-middleware), or use a callable (it receives the same arguments as the `MiddlewareInterface::process()` method). |
# |
Jun 17th 2021, 13:54 |
erwane |
Thanks. yes, i can pass it in template path, but i'm looking for a more generic way, just pass the template, Locale is "another logic". I will create a "MailerView" |
# |
Jun 17th 2021, 13:52 |
ndm |
@erwane I don't think `setOptions()` will work, if it's not in `$_passedVars`, then you can't pass it via the view builder. However you should be able to achieve the same result by modifying the template path accordingly. |
# |
Jun 17th 2021, 13:49 |
alamnaryab |
@ndm I do not know how to add my custom code or logic like this in middleware currently I can see only chained add functions to middleware queue. |
# |
Jun 17th 2021, 13:47 |
slackebot |
`View::$subDir` through viewBuilder()->setOption() ? |
# |
Jun 17th 2021, 13:47 |
erwane |
Hi, I'm trying to create email with localized templates. The `View` has subDir option now, perfect, but this option is not in ``` /** * List of variables to collect from the associated controller. * * @var string[] */ protected $_passedVars = [ 'viewVars', 'autoLayout', 'helpers', 'template', 'layout', 'name', 'theme', 'layoutPath', 'templatePath', 'plugin', ];``` We can't define |
# |
Jun 17th 2021, 13:45 |
ndm |
Make sure that the middleware is added _after_ the routing middleware, otherwise the parameters will never be present. |
# |
Jun 17th 2021, 13:44 |
ndm |
@alamnaryab It should be _relatively_ straightforward, test if the language parameter is present (like the in the URL filter), and if it's not, return a response with a location header that holds the current URL with whatever language you want to redirect to prepended. |
# |
Jun 17th 2021, 13:41 |
kevin.pfeifer |
but I would guess if you prefer a more frontend AJAX UX you could look at this https://github.com/ishanvyas22/cakephpvue-spa |
# |
Jun 17th 2021, 13:34 |
kevin.pfeifer |
then you will need to render that element with the entity you already have and return that html via ajax |
# |
Jun 17th 2021, 13:33 |
kevin.pfeifer |
so you could create an element which represents one row of your table |
# |
Jun 17th 2021, 13:33 |
kevin.pfeifer |
I have loads of elements which are re-used over and over inside my template |
# |
Jun 17th 2021, 13:32 |
kevin.pfeifer |
elements are reusable parts you use in your template https://book.cakephp.org/4/en/views.html#elements |
# |
Jun 17th 2021, 13:32 |
dantedantas |
Elements? Sorry, I dont know what do you talk about. :( |
# |
Jun 17th 2021, 13:31 |
dantedantas |
Maybe I could run a query, collect the result and create the HTML code and send it back. |
# |
Jun 17th 2021, 13:31 |
kevin.pfeifer |
you know what “elements” are in cakephp? |
# |
Jun 17th 2021, 13:30 |
dantedantas |
Like this it works. |