I'm new to laravel and still learning about this framework.
I already found some questions on stackoverflow but it still didn't work out for me.
My problem is:
I got this
localhost/codehub/public/users/create
and the route:
Route::get('users/create',['uses' => 'UserController#create']);
Inside the page there's some form like this:
so when I click create button it is supposed to route it into store function in the user controller
Route::post('users',['uses' => 'UserController#store']);
public function store(Request $request)
{
return $request->all();
}
so the problem is when I click that create button it always redirects me to localhost/users and because of that, I can't process my store function.
Any advice?
this is my form code:
<form method="post" action="/users">
<input type="text" name="name">
<input type="email" name="email">
<input type="password" name="password">
<input type="submit" value="Create">
</form>
The problem may be because of relative path in form action.
You should always use named routes which allow the convenient way of generation of URLs or redirects for specific routes.
So you can change your route as:
Route::post('users', 'UserController#store')->name('users.create');
And in form you can write as:
<form method="post" action="{{ route('users.create') }}">
Related
I'm trying to create a little search bar on my website with symfony 4. How do I get the the user input data from the form which can be used in the controller. The form looks like this :
//.../navbar.html.twig
<form action="search" method="get" >
<input type="text" placeholder="Search..">
<button type="submit" class="btn btn-default">Search</button>
</form>
Since you don't seem to be using a FormType and your form's method is 'GET':
First your need a name to your input. ex:
<input type="text" name="search" placeholder="Search..">
Then just pass the request service to your action in your controller and get the desired parameter.
public function yourAction(Request $request){
$searchString = $request->get('search');
}
Edit: I strongly recommend using symfony's form component tho. Doc here : https://symfony.com/doc/current/forms.html
I am trying to make a simple view and I want to be able to make a post request. I need to generate a csrf token and this is the html view that I have:
<form method="POST" action="/formsubmit">
{!! csrf_field() !!}
First Name: <br>
<input type="text" name="firstname"><br>
Last Name:<br>s
<input type="text" name="lastname"><br>
<input type="submit" name="Submit"><br>
</form>
This is my route:
Route::get('form', function(){
return view('form');
});
Route::post('formsubmit',function(){
return 'Form Posted.';
});
Auth::routes();
This is what happens when I try the url:
As you can see, it just prints the name of the function, but the function is not ever called and the hidden field is not being generated. Is there something that needs to be done in order to make it work?
Blade's templating language will only get interpreted in a file with a .blade.php extension. One with just a .php extension will work with Laravel, but you won't have any Blade functionality.
I'm having a very frustrating (super-newbie) experience with Laravel 5.1.
Sure there must be something that I'm missing, but unfortunately I couldn't find anything on Laravel docs.
The problem is this (and I believe is even relatively simple): while all GET routes are working, the routes in POST are 'rerouting' me to the wrong place.
For instance, assuming that the controller is this:
namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;
use View;
class PagesController extends BaseController {
public function hello(){
return view('hello');
}
public function register(){
//Registration rules
$inputData=array(
'name' => \Input::get('name'),
'email' =>\Input::get('email'),
'password' => bcrypt(\Input::get('password')),
);
createUser($inputData);
return view('stat');
}
private function createUser($inputData){
return User::create($inputData);
}
}
and given the route:
Route::get('/','PagesController#hello');
this one redirects me correctly to the view specified in the controller,at the method indicated.
A POST operation cannot be successfully performed, since
Route::post('/','PagesController#register');
with the form having:
<form action="/" method="post">
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
<label for="name">User Name</label><br/>
<input type="text" name="name" id="name"/><br/>
<label for="name">E-mail</label><br/>
<input type="text" name="email" id="email"/><br/>
<label for="password">password</label><br/>
<input type="password" name="password" id="password"/><br/>
<input type="submit" value="auth!"/>
</form>
results in redirecting me to xampp home page.
For example, the "main registration page" is at this url:
http://localhost:8080/laravel/project1/public/
(GET OK)
and when I click on the submit button, I'm sent to
http://localhost:8080/
Just few more indicators for you to try and help me (I'm genuinely stuck):
1) I didn't use a VirtualHost (searched on the web, but had always no luck in configuring apache properly...good luck I've got xampp)
2) The page "hello" has a link that I used as a test:
Link
and it redirects me correctly to the sought-after
http://localhost:8080/laravel/project1/public/stat
3)In the POST method, I've used a dd($inputData) to see what was going wrong, and as a result I had...nothing. Not a blank page,just always the localhost page. This led me to think that somehow the controller method isn't called, since no dd(---) result got in page.
Hope someone can help.
Many thanks
Your domain is http://localhost:8080 , so when you set action to '/' , it goes to root. It`s not good practice , but your solution is to change :
<form action="/laravel/project1/public/" method="post">
I`d create a virtual host for the project in apache , so document root would be /laravel/project1/public/ .
you are posting your form to the root / that is why you are being routed to xampp page. To solve this problem, change your form action to this
<form action="/post/my/form" method="post">
in your routes
Route::post('/post/my/form','PagesController#register');
Hi in my login page I have forgot password link. From where I have to send the reset password links to the users. I hope I did everything correctly, but still I am getting the " MethodNotAllowedHttpException " Error.
HTML Code
<form action="/user/sendresetlink" method="post" id="forgot_password_form" name="forgot_password_form">
<label for="name" class="col-xs-4 control-label">User Name</label>
<input type="text" id="user_name" name="user_name" class="form-control" />
<button type="submit" class="btn bg-olive btn-block">Send</button>
</form>
Router Code
Route::resource('user', 'UserController');
Here I Have mentioned resource for UserController, where laravel takes care of basic CRUD routings.
Route::get('login', 'UserController#create');
Route::post('/user/store','UserController#store');
Route::get('logout', 'UserController#destroy');
Route::get('forgot_password','UserController#forgotPassword');
Route::post('sendresetlink','UserController#sendResetLink');
I have mentioned the sendresetlink as post and calling the controller. It is not even going to controller.
Route::group(array('before' => 'auth'), function()
{
Route::get('/jobs', 'JobsController#jobs_list');
});
Controller Code
public function sendResetLink()
{
$form_data = Input::all();
echo '<PRE>';
print_r($form_data);
exit;
}
What am I doing wrong here? Am I missing anything?
Note: I have installed laravel in another machine and copied over the code to current machine. May be because of that, my php artisan is not working. When ever I tries php artisan in command prompt, it is stating that 'php' is not recognized as any internal external command. I tried to install the composer in the php.exe folder. Even then also no use.
At app/routes.php you have written
Route::post('sendresetlink','UserController#sendResetLink');
While at the form action you have
<form action="/user/sendresetlink" method="post" id="forgot_password_form" name="forgot_password_form">
You can fix this by changing app/routes.php to
Route::post('user/sendresetlink','UserController#sendResetLink');
There is a miss-match between your route and your form action.
/user/ sendresetlink and just sendresetlink.
Replace
Route::post('sendresetlink','UserController#sendResetLink');
With
Route::post('/user/sendresetlink','UserController#sendResetLink');
and it will work just fine.
Explanation:
Your form actionis <form action="/user/sendresetlink" ...
Same should be matched with the URL parameter of Route::POST as shown above.
I have form like this
<form action="{{ Request::root() }}/articles/update/" method="post">
<input type="hidden" name="id" value="{{ $article->id }}" />
<input type="submit" name="submit" value="Submit" />
</form>
And route like this
Route::post('articles/update', array('as' => 'articleUpdate', 'uses' => 'ArticlesController#update'));
But when I submit the form, I get MethodNotAllowedHttpException. In error report I can see that request method is GET. I have also tried using caps for method method="POST" but it didn't work.
Any ideas?
What does FireBug/Web console inspector show you? is the form being sent via GET or POST, any redirects?
Seems a redirection problem to me, after reaching the server Laravel redirects to the URL the form sent the post request.
you must use put method here. Form change like this
{{Form::open(array('url'=>'/articles/update','method' => 'PUT'))}}
Routes like this
Route::put('/articles/update','ArticlesController#update');