# |
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. |
# |
Jun 17th 2021, 13:30 |
dantedantas |
I am not sure how to do it, or what should be the best way. |
# |
Jun 17th 2021, 13:26 |
kevin.pfeifer |
but how should cakephp know that you now want to render your entity in your ajax function with that HTML? |
# |
Jun 17th 2021, 13:25 |
kevin.pfeifer |
yes that is present in your index.php |
# |
Jun 17th 2021, 13:25 |
kevin.pfeifer |
I never use AJAX functions like that because it is (in my opinion) too much data being sent via AJAX. I only send the data that is being “added/changed” and the HTML is being copied on the clientside inside the JS |
# |
Jun 17th 2021, 13:25 |
dantedantas |
This is how I show it now:' |
# |
Jun 17th 2021, 13:23 |
dantedantas |
Here is where I show the result. If I could send back a formated HTML code, could be better. |
# |
Jun 17th 2021, 13:23 |
kevin.pfeifer |
and where should cakephp get that html from? you only have an entity in that controller, not a rendered template |
# |
Jun 17th 2021, 13:21 |
dantedantas |
Could be better. A Html. |
# |
Jun 17th 2021, 13:21 |
kevin.pfeifer |
do you expect to get a formatted HTML back? |
# |
Jun 17th 2021, 13:20 |
dantedantas |
No. :) Maybe, could you have an example of how to format it? |
# |
Jun 17th 2021, 13:20 |
alamnaryab |
I am trying to see documentation how to capture request in middleware and append default language param if not available |
# |
Jun 17th 2021, 13:17 |
kevin.pfeifer |
yes, do you expect something different? |
# |
Jun 17th 2021, 13:16 |
dantedantas |
The result. |
# |
Jun 17th 2021, 13:16 |
dantedantas |
Is already. |
# |
Jun 17th 2021, 13:15 |
kevin.pfeifer |
|
# |
Jun 17th 2021, 13:14 |
kevin.pfeifer |
have you changed your AJAX request to GET? |
# |
Jun 17th 2021, 13:14 |
dantedantas |
Hi Kevin. Thank you for your feedback. It looks much better. :) The 'IF' condition still doesnt work. |
# |
Jun 17th 2021, 13:12 |
ndm |
I see, looks like they're supported in 4.next |
# |
Jun 17th 2021, 13:06 |
admad |
Maybe, a while back I updated the router classes to support brace style everywhere (and check for those first) |
# |
Jun 17th 2021, 13:05 |
ndm |
Maybe because brace style placeholders aren't supported, only the colon ones work. |
# |
Jun 17th 2021, 13:04 |
ndm |
@alamnaryab That is the expected behavior, the URL filter only persists the language parameter if there is one present in the request. You could add a default or inferred (from the request's `Accept-Language` header) language in the URL filter, or possibly the better solution, use a middleware to redirect to a URL with a language parameter being added to the path, so that you don't have one and the same URL serving different content. |
# |
Jun 17th 2021, 12:58 |
admad |
I was mistaken about not being able to use placeholder as scope :) There was probably some other reason why I didn't/couldn't use them for my i18n routes and misremembered. |
# |
Jun 17th 2021, 12:53 |
alamnaryab |
@ndm it is all working fine as long the URL has language param but without language param is says missing param |
# |
Jun 17th 2021, 12:22 |
ndm |
Then I misunderstood, I thought your problem was the other way around with 4.x, ie a string _not_ working. Converting the data to a string with `http_build_query()` should be fine, it's what happens internally too. |
# |
Jun 17th 2021, 12:20 |
alamnaryab |
@ndm This is working perfectly. You are the hero. :thumbsup: |
# |
Jun 17th 2021, 12:08 |
slackebot |
\Cake\Http\ServerRequest $request) { if ( $request->getParam('language') andand !isset($params['language']) ) { $params['language'] = $request->getParam('language'); } return $params; } );``` |
# |
Jun 17th 2021, 12:08 |
slackebot |
$patterns = ['language' => 'en|ar']; $builder ->connect('/', ['controller' => 'Clients', 'action' => 'index']) ->setPatterns($patterns); $builder ->connect('/:controller', ['action' => 'index']) ->setPatterns($patterns); $builder ->connect('/:controller/:action/*') ->setPatterns($patterns); }); \Cake\Routing\Router::addUrlFilter( function (array $params, |
# |
Jun 17th 2021, 12:08 |
slackebot |
'en|ar']; $routes ->connect('/', ['controller' => 'Users', 'action' => 'login']) ->setPatterns($patterns); $routes ->connect('/:controller', ['action' => 'index']) ->setPatterns($patterns); $routes ->connect('/:controller/:action/*', []) ->setPatterns($patterns); }); }); $routes->scope('/:language', function (RouteBuilder $builder) { |
# |
Jun 17th 2021, 12:08 |
ndm |
@alamnaryab Go from here. If you run into errors, don't jump to using other methods until you have a firm grasp on why things are failing and how the problem could be solved, otherwise you'll never get anywhere with it, as you're just jumping from one problem to the next. ```$routes->scope('/:language', function (RouteBuilder $routes) { $routes->prefix('Admin', function (RouteBuilder $routes) { $patterns = ['language' => |