Comments (14)
You can Return a redirect, is that what you are looking for? On successful login you can return a redirect to the dashboard, or on failure you can return a redirect back to the login page.
from twig-view.
Something like that but I want also to pass some data/errors to that page, how could I do that?
from twig-view.
You can use Slim-Flash or another Flash message package to set the error and then retrieve it from the next page view (which is the result of the redirect). You might also store the error/data in the session and then clear it, which is essentially what Slim-Flash is doing.
from twig-view.
Something like this...
$this->flash->addMessage('errors', 'Your username or password is incorrect.');
return $response->withRedirect('/login');
from twig-view.
Great! I got it working with Slim-Flash and slim-twig-flash.
But there is one more thing I want to do... This works great for showing messages but what if I want to show also error on specific input?
from twig-view.
Like applying a specific class to an input field? When I pass $errors
to a view, it is an associative array with field names and error messages. My Twig views might look like this.
<div class="input-group{% if errors.age %} has-error{% endif %}">
from twig-view.
Okay, I think I got this. I'm using Violin for validation and I'm trying somehow to format their messages in view, but I'll probably end up using some helper class.
Thank you so much @tflight
from twig-view.
It doesn't look like Violin has been updated in a couple of years. I use Valitron and get the error messages from it fairly easily. When I pass the data to a view I'll do something like
$data = [
'somemodel' => $model,
'someUser' => $user,
'errors' => $validator->errors()
];
from twig-view.
Hmm, thanks, I will switch to that one as I just started the project. In your example, are you passing the whole $data in flash? Can you paste here piece of the actual project code where you are returning errors and other data in view?
from twig-view.
I'm not using Slim-Flash, so that might be part of what is different. I'm using a different flash messages package. But no, I'm not passing $data
to my flash message, just the specific message along the lines of "Oops, check you're work". That is all I put into flash. The $data
goes into the view, and from there I have a Twig partial that checks if an 'errors' variable exists, and if so loops through them which are the errors from Valitron. (Running out for the day now, but I can drop you some code tomorrow if that isn't clear.)
from twig-view.
Yeah, I figured out that part. What I'm wondering then how are you handling rendering view from controller or more specifically passing data to the view from controller (my initial question)?
I'm looking in Bookshelf example app, and I see that they do it with $this->view->render but as I said earlier, it does opens up correct view but URL stays the same, it doesn't go by the routes.
I can also see, example app is using the same function for editAuthor GET and POST, is this how you normally do in Slim? And I see why will this return correct URL and view but what if I want to show some other view and pass data while in different controller?
from twig-view.
I guess I'll be using same the route name for GET and POST when I need to return additional data to the same route. Thanks once again @tflight for your time and patience :)
from twig-view.
In the bookshelf app, they pass data to the view from the controller her.
You are correct that it when returning withRedirect
it will only accept the URL to redirect to and a status code. (Part of the stateless nature of the web.) So you could add the data you wish to send to the redirected view from a session, perhaps using a flash message. Assuming your session storage message can handle objects, you could add a new flash message with a string, array, or object. Then in the controller that responds to that redirect, check if there is a flash message with the key that contains the array or object, and adjust how the controller responds accordingly.
I typically have different actions (controller methods) respond to GET and POST requests, unlike the bookshelf example. I tend to follow the structure here.
from twig-view.
Yeah, I'm following the same routing structure here too. Thanks
from twig-view.
Related Issues (20)
- Question - Twig 3 Support HOT 10
- Slim\Views\Twig::create does not exist HOT 3
- Add support for Twig 3 in 3.x branch HOT 1
- Slim 4 Compatibility In The Pipeline? HOT 1
- Rendering no space HOT 1
- Fix incorrect declaration HOT 3
- Version on Readme HOT 1
- Twig could not be found in the server request attributes using the key "view" HOT 5
- ,
- How to get args for is_current_url()? HOT 2
- Using view outside a request/response cycle does not load the Slim/Views/TwigExtension HOT 2
- Trying to use TwigExtension template functions returns an error HOT 1
- Uncaught Twig\Error\SyntaxError: Unknown "defined" test. HOT 3
- is_current_url() doesn't take basepath into consideration HOT 20
- Support PHP 8.0
- twig->fetch() outputs to browser rather than return string HOT 8
- 3.3.0 Release date HOT 3
- `composer require slim/twig-view:^3.0` handle PHP Version Problem HOT 1
- Rendering a template from a middleware HOT 3
- Error using Twig with Slim 4 & Container HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from twig-view.