Laravel Form POST redirect failure - php

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');

Related

Assist with using native html form in laravel

Hello: I have done tons of html forms in wordpress and am now trying to do the same in laravel. The problem is with submitting the form; it is not going to my destination or passing the post variables as I would expect. (i am getting errors on the loading of the page).
I know it has something to do with the "routes" and possibly also CSRF? (been reading a lot on this and seeing all kinds of info
I have seen things about using Laravel to build a form "open form/close form" but I am trying to find a way to just use an html form
I have the default laravel installed with nothing extra...
I tried adding a "post" route but that did not help...
here is what i have now:
this is from my routes.php file:
Route::post('gz_form', ['as' => 'gz_form', 'uses' => 'cont15_gzap#gzap_cont_function']);
here is the top of my form:
<form method="post" autocomplete="off" action="{{ route('gz_form') }}" >
<input name="_token" type="hidden" value="{{ csrf_token() }}"/>
<input type="hidden" name="gc_post" value=2 />
(I threw on that token input as some people suggested that...)
Anyway - I am hoping someone can help me with this...
you have to check your route method .. is it post or get .. and check if your route named already or not ..
Route::post('/gz_form', 'YourController#handler')->name('gz_form');
while you using {{ route('gz_form') }} you need to name it
I was able to get this to work using both of your inputs - but once i had it down to the token mismatch - i finally got it to work by changing the input on my form to:
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
i found that somewhere and by doing that the token mismatch went away...
But thanks for your help guys - getting this to work took more than one step and so your help was needed and helpful...

getting laravel local host path error (object not found)

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') }}">

MethodNotAllowedHttpException in Laravel 4.2

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.

search url manager in Yii

I implemented my project in Yii. i done URL management its working fine. but i search URL is not being proper. i added my code here. please tell me how to change this code according to my URL..
my main.php:
<form class="navbar-form " role="search" name="searchform" method="POST" action="<?php echo Yii::app()->baseUrl.'/index.php/recipe/course1/';?>" id="menu-form-style" onsubmit="return ValidateSearchForm();">
<div class="form-group">
<input type="text" name="search" id="search" placeholder="Quick Search...." class="form-control" onkeyup="lookup(this.value);" onblur="fill();">
i sent to my recipe controller.
where i did like this: recipe controller
public function actionCourse1(){
if(isset($_POST["search"]))
$course=$_POST["search"];
$this->redirect(array('recipe/course','searched'=>$course));}
public function actionCourse(){
$model=new Recipe;
if(isset($_GET['searched']))
$count=$_GET['searched'];
$bigArray=array();
in my configure main.php
'searched'=>'recipe/course',
above config line i wants to be show.
but am getting like this
search/searched/asdfadfadfa
http://kitchenking.ebhasin.com/
this our website. please suggest me suitable answer
if i understod your question true try this...
$this->render('/recipe/course/ or your file place', array('searched'=>$course));}
or
$this->redirect('/recipe/course/searched/'.$course);
Re-read http://www.yiiframework.com/doc/guide/1.1/en/topics.url this one, yes I know that is not an answer, but I think your routes is invalid.
change your config main.php:
'search/<searched>'=>'recipe/course',

CodeIgniter form submit redirects to localhost

I have a CodeIgniter project with a very simple test application.
Controller opens a view (PHP page) which has a text box and a submit button.
When I press the submit button, instead of redirecting to the appropriate function call in the controller, I get redirected to localhost/xampp.
here's the code on my view, which SHOULD be redirecting to the save_genre function in the controller named "welcome".
<?php echo form_open('welcome/save_genre');?>
<label for="radio_genre">Radio Genre</label>
<input type="text" name="radio_genre" id="radio_genre"></input>
<?php echo form_submit('submit','Save'); ?>
</form>
any ideas what could be wrong? I think it's not the code but is a setting or file wrong somewhere, but i don't know where to start looking.
EDIT:
I had already redefined the base URL in the config file.
I don't think I rewrote the .htaccess - but I'll certainly check. (This is a team project setup and I'll make sure no one else has done that.)
As requested, below is the HTML output by the form. The URL link for the form seems very odd to me because it doesn't mention the project name like I would expect. So there are two places for me to look now. Thanks!
<body>
<h1>Welcome!</h1>
<form action="http://localhost/index.php/welcome/save_genre" method="post">
<label for="radio_genre">Radio Genre</label>
<input type="text" name="radio_genre" id="radio_genre"></input>
<input type="submit" name="submit" value="Save" />
</form>
</body>
EDIT: OK - I recreated the project myself and then brought my PHP files in and it works fine. So the person who created the project did something odd.
You need to edit $config['base_url'] in /system/application/config/config.php.
Somehow, while the index.php file was there in the file system, it wasn't being recognized as part of the project. Once I copied a new index.php file in there (identical file but it trigger the sense that "this changed.), I then went back into my project and refreshed and saved.
Everything then worked.

Categories