G
G
ganjo8882019-09-03 16:13:13
symfony
ganjo888, 2019-09-03 16:13:13

How to properly use symfony component route?

I am just starting my programming journey.
I am writing a small project on symfony components.
I have such a route

index:
  path: /
  defaults:
    _controller: 'App\Controllers\CommentController::get'

where I display all comments
also on the main page I have a form for adding a comment
<form action="/" method="POST" enctype="multipart/form-data">
                <section class="form-gradient mb-5">
                    <div class="header peach-gradient">
                        <div class="row d-flex justify-content-center">
                        </div>
                    </div>
                    <div class="card-body mx-4">
                        <div class="md-form">
                            <input type="text" id="form104" name="name" class="form-control" placeholder="Ваше имя"
                                   required>
                        </div>
                        <div class="md-form">
                            <input type="text" id="form105" name="email" class="form-control" placeholder="Ваша почта"
                                   required>
                        </div>
                        <div class="md-form">
                            <input id="form107" name="text" class="md-textarea form-control" rows="5"
                                   placeholder="Добавьте Ваш комментарий" required>
                        </div>
                        <div class="row d-flex align-items-center mb-3 mt-4">
                            <div class="col-md-12">
                                <div class="text-center">
                                    <button type="submit" class="btn btn-grey btn-rounded z-depth-1a">Send</button>
                                </div>
                            </div>
                        </div>
                    </div>
        </div>
        </section>
        </form>

my controller
class CommentController extends Controller
{
    public $model;

    public function __construct(CommentModel $model)
    {
        parent::__construct();
        $this->model = $model;
    }

    public function set(Request $request)
    {
        $name = $request->request->get('name');
        $email = $request->request->get('email');
        $text = $request->request->get('text');

        $this->model->setComment($name, $email, $text);
    }

    public function get(Request $request)
    {
        $data = $this->model->getComments();
        $this->view->generate('chat_view.php', 'home_view.php', $data);
    }
}

How can I handle two methods of the same controller on the same page?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BoShurik, 2019-09-03
@ganjo888

index:
  path: /
  defaults:
    _controller: 'App\Controllers\CommentController::get'
  methods: ['GET']

set:
  path: /
  defaults:
    _controller: 'App\Controllers\CommentController::set'
  methods: ['POST']

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question