For example, I have this route in my web.php :-
Route::get('products/{product}/owners', 'ProductController#showOwners');
When I try to add new 'Owner' to the product, I have to do it in the parent URL, like this :-
Route::post('products/storeOwner', 'ProductController#storeOwner');
And then I pass the product ID in a hidden field in the form, because the post request doesn't accept URL parameters. So is there anyway to do it like below ?
Route::post('products/{product}/storeOwner', 'ProductController#storeOwner');
So the POST request will be sent inside the particular 'product' URL?
UPDATE
/* ProductController Class */
public function storeOwner (AddProductOwner $request)
{
$product= Product::find($request->product);
$user = Auth::user();
if ( $user->ownerOf($product)) {
// Check if the current user is already one of the owners).
// If the current user is the owner then return to the product
// This line is not executed because in (products/show.blade.php) we have set a condition.
return redirect('products/' . $request->product);
}
$join = new Join;
$join->role = $request->join_role;
$join->product()->associate($request->product);
$join->user()->associate(Auth::user());
$join->message = $request->message;
$join->save();
// TODO: we have to make this with ajax instead of normal form
return redirect('products/'. $request->product);
}
I hope my question is clear enough..
Yes you can do as you mentioned in your last route
Route::post('products/{product}/storeOwner', 'ProductController#storeOwner');
And then get the product Id in your functions argument
public function storeOwner (AddProductOwner $request, $productId)
{
dd($productId); // TRY THIS OUT. CHECK THE 2nd ARGUMENT I SET.
$product= Product::find($productId); // PASS THE VERIABLE HERE.
$user = Auth::user();
if ( $user->ownerOf($product)) {
// Check if the current user is already one of the owners).
// If the current user is the owner then return to the product
// This line is not executed because in (products/show.blade.php) we have set a condition.
return redirect('products/' . $request->product);
}
$join = new Join;
$join->role = $request->join_role;
$join->product()->associate($request->product);
$join->user()->associate(Auth::user());
$join->message = $request->message;
$join->save();
// TODO: we have to make this with ajax instead of normal form
return redirect('products/'. $request->product);
}
You can send URL parameters to a POST request. Just make sure in your form you are sending the wildcard.
<form action="/products/{{ $productid }}/storeOwner" method="POST">
In your routes
Route::post('products/{productid}/storeOwner', 'ProductController#storeOwner');
In your controller, use it
public function storeOwner($productid)
{
dd($productid);
}
Related
I am trying to change the url of my article page so instead of showing the article id I'd like to show the article title.
Currently the URL is as follows;
https://www.example.com/posts/post/32
Where 32 is just a random article id.
Instead I like it to display as follows;
https://www.example.com/posts/post/my-amazing-article
Now I have looked in the laravel documentation and different posts on stackoverflow and tried a bunch of stuff but I'm obviously making a mistake somewhere cus nothing seems to work so im pretty much back where I started.
Blade
Route
Route::get('/posts/post/{id}', [App\Http\Controllers\WelcomeController::class, 'post'])->name('post');
Controller
public function post(request $request){
$id = $request->id;
$article = Tinymce::find($id);
return view('/post')->with('articles, $articles');
}
Now the articles, which are saved in Tinymce, are actually created on a different controller on subdomain.
public function tinymce(Request $request)
{
if(request()->ajax())
{
if($request->article_id == null)
{
$tinymce = new Tinymce;
}else{
$tinymce = Tinymce::find($request->article_id);
}
$tinymce->description = $request->description;
$tinymce->content = $request->myContent;
$tinymce->title = $request->title;
$tinymce->author = $request->author;
$tinymce->publish = $request->publish;
$title = $tinymce->title;
$slug = Str::slug($title, "-");
$tinymce->slug = $slug;
$tinymce->save();
return $tinymce->id;
}
}
As you can see I've turned the title into a slug as I read somewhere that's the way to go to use custom url but I didn't get very far with it.
Any help would be greatly appreciated, thank you.
Explanation:
First, As you said you stored slug into your database. so, that's good.
From Controller to View, you can get that slug into post object.
Blade View : (you have to pass slug in route url)
Route : (make id to slug)
Route::get('/posts/post/{slug}', [App\Http\Controllers\WelcomeController::class, 'post'])->name('post');
Controller : (now you can get slug from the second parameter)
public function post(request $request, $slug = ''){
$article = Tinymce::where('slug', $slug)->first();
return view('/post', compact('article'));
}
Now, you can use access custom URLs using slug.
I Hope, it helps you. #assiemp
I'm encountering a problem where a redirect from one route to another is calling the targeted controller method twice. This question addresses a similar issue, but the OP passing a 301 status code was deemed to be the issue in the accepted answer, and I'm not specifying any status code. I'm also using the session state for parameters. The relevant code looks something like this:
public function origin(Request $request) {
// Assume I have set variables $user and $cvId
return redirect()
->action('SampleController#confirmUser')
->with([
'cvId' => $cvId,
'userId' => $user->id,
]);
}
public function confirmUser(Request $request) {
$cvId = session()->get('cvId');
$userId = session()->get('userId');
if (is_null($cvId) || is_null($userId)) {
// This is reached on the second time this is called, as
// the session variables aren't set the second time
return redirect('/home');
}
// We only see the view for fractions of a second before we are redirected home
return view('sample.confirmUser', compact('user', 'cvId'));
}
Any ideas what could be causing this? I don't have any next middleware or any of the other possible causes that are suggested in related questions where controllers are executed twice.
Thanks for any help!
Have you tried passing values in parameters? Try the below code.
public function origin(Request $request) {
// Assume I have set variables $user and $cvId
return redirect()->action(
'SampleController#confirmUser', ['cvId' => $cvId, 'userId'=>$user->id]
);
}
public function confirmUser(Request $request) {
$cvId = $request->cvId;
$userId = $request->userId;
if (is_null($cvId) || is_null($userId)) {
// This is reached on the second time this is called, as
// the session variables aren't set the second time
return redirect('/home');
}
// We only see the view for fractions of a second before we are redirected home
return view('sample.confirmUser', compact('user', 'cvId'));
}
Currently I'm working on a project where I made it so that when a user types a correct password in form field, it will give them the items from the given section.
The main problem i'm having is that to do this I need to capture the request and therefore the route has to be a post method instead of a get as such:
public function index(Request $request)
{
$id = $request->input('id');
$password = $request->input('password');
$result = DB::table('scrumboards')->find($id);
if ($result->key == $password) {
$scrumboard = $result;
$items = DB::table('backlogs')->get();
return view('scrumboard', ['items' => $items, 'scrumboard' => $scrumboard]);
} else {
$scrumboard = $result;
return redirect('home');
}
}
and the route as such:
Route::post('/scrumboard', 'ScrumboardController#index');
By doing this, request errors wont work since It wants to redirect back but can't since this is a post method.
Any way I can avoid this clash?
Routes can have multiple HTTP verbs. Define your route as
Route::match(['get', 'post'], '/scrumboard', 'ScrumboardController#index');
to make it available as GET and POST route.
I have form like this :
name (required).
slug (required).
slug is required in back end but user is allowed to leave it blank in form field ( if user leave slug blank, it will use name as the input instead ).
I have tried with Event form listener but it said You cannot change value of submitted form. I tried with Data transformers like this :
public function reverseTransform($slug)
{
if ($slug) {
return $slug;
} else {
return $this->builder->get('name')->getData();
}
}
return $this->builder->get('name')->getData(); always return null. So I tried like this:
public function reverseTransform($slug)
{
if ($slug) {
return $slug;
} else {
return $_POST['category']['name'];
}
}
it works but I think it against the framework. How I can done this with right way?
You can also do it in the controller
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
// get the data sent from your form
$data = $form->getData();
$slug = $data->getSlug();
// if no slug manually hydrate the $formObject
if(!$slug)
{
$formObject->setSlug($data->getName());
}
$em->persist($formObject);
$em->flush();
return ....
}
}
If you use a function to keep the code at one place then you should also not work with Request data.
In the form action you call that function including the name variable.
public function reverseTransform($name, $slug)
{
if (!empty($slug)) {
return $slug;
} else {
return $name;
}
}
Another possible way is to set via request class like this:
Array form <input name="tag['slug']"...>:
public function createAction(Request $request)
{
$postData = $request->request->get('tag');
$slug = ($postData['slug']) ? $postData['slug'] : $postData['name'];
$request->request->set('tag', array_merge($postData,['slug' => $slug]));
.......
Common form <input name="slug"...>:
$request->request->set('slug', 'your value');
I think this is the best way because if you are using dml-filter-bundle you don't need to filter your input in your controller like this again:
$this->get('dms.filter')->filterEntity($entity);
I am sure I am going about this the wrong way, but I need to unset an array key from one of my choices in a sfWidgetFormChoice. The only way to get that variable to the Form is from the action. Here's what I have:
Action:
$id = $request->getParameter('id');
$deleteForm = new UserDeleteForm();
$choices = array();
$choices = $deleteForm->getWidgetSchema('user')->getAttribute('choices');
unset($choices[$id]); //I obviously don't want the user to be able to transfer to the user being deleted
$this->deleteForm = $deleteForm;
Form:
$users = Doctrine_Core::getTable('sfGuardUser')->getAllCorpUsers()->execute();
$names = array();
foreach($users as $userValue){
$names[$userValue->getId()] = $userValue->getProfile()->getFullName();
};
// unset($names[$id]); //this works, but I can't figure out how to get $id here.
$this->widgetSchema['user'] = new sfWidgetFormChoice(array(
'choices' => $names
));
$this->validatorSchema['user'] = new sfValidatorChoice(array(
'required' => true,
'choices' => $names
));
Understanding forms and actions:
Usually we will setup a form with fields, print it in a html page and fill the form with data. Pressing the submit form button will send all the data to a method defined in your form action html attribute.
The method will receive and get a $request , with a lot of parameters and also the form with the data. Those values will be processed in the action.
Lets look how it exactly works in symfony:
Define and Setup a symfony form, like the one you have shown above.
Print the form and in the action parameter point to the submit method
which will receive the request:
<form action="currentModuleName/update"
Symfony will automatically send the request to the action.class.php
of your module, and will look for and send the data to the function
executeUpdate
public function executeUpdate(sfWebRequest $request){ //...
$this->form = new TestForm($doctrine_record_found);
$this->processForm($request, $this->form); }
After some checks, symfony will process the form and set a result
template.
processForm(sfWebRequest $request, sfForm $form)
{ ... } $this->setTemplate('edit');
In the processForm of your module action.class.php, you should process all the received values (request) also with the form:
protected function processForm(sfWebRequest $request, sfForm $form)
{
$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
if ($form->isValid())
{
$formValues = $this->form->getValues();
$Id = $formValues['yourWidgetName'];
}
}
You may check the following link for an example like yours, about how to process a sfWidgetFormChoice.
And now answering to the real question, in order to select the deleted users, add the following code in your action:
//process the form, bind and validate it, then get the values.
$formValues = form->getValues();
$choicesId = $formValues['choices'];
Pass variable from action to the form:
Excuse me if I have not understand your question at all but in case you need to pass some parameters from your action to the form, send the initialization variables in an array to the form constructor:
Pass a variable to a Symfony Form
In your case, get the list of users, delete the user you dont want and send the non deleted users to the form constructor.
You will need to redeclare/overwrite your form again in the configure() function so that you could change the initialization of the form. Copy and paste the same code into the configure() function and comment the line: //parent::setup();
class TbTestForm extends BaseTbTestForm
{
public function configure()
{
//.. copy here the code from BaseTbTestForm
//parent::setup();
$vusers = $this->getOption('array_nondeleted_users');
//now set the widget values with the updated user array.
}
}