I must be missing something really simple but I can't seem to find it. So I have my Resource defined in my routes.php but I need an additional route for an advanced search page with filters and stuff, my show/update/edit/create... pages are working perfectly but my search page isn't.
So in routes I have:
Route::resource('hostesses', 'HostessesController');
Route::get('/hostesses/search', 'HostessesController#search');
And I have a search form on my main page like this:
{{ Form::open(array('route' => 'hostesses.search', 'class' => 'navbar-form navbar-right')) }}
<div class="form-group">
{{ Form::text('search_term', '', array('class' => 'form-control')) }}
</div>
{{ Form::submit('Submit', array("class"=>'btn btn-default')) }}
{{ Form::close() }}
And when I use the search form I get the NotFoundHttpException
In my controller I have:
public function search()
{
return View::make('hostesses.search');
}
And I have created the template on views/hostesses/search.blade.php with a simple hello world message to check that it works, but I keep getting the exception!
Change the order of your routes and 'define' the named route of hostesses.search which is in your form
Route::any('/hostesses/search', array(['as' => 'hostesses.search', 'uses' => 'HostessesController#search');
Route::resource('hostesses', 'HostessesController');
Because what is happening is the resource for /hostesses/$id is capturing the search id, and returning an error that id of search does not exist
Also - change your route to Route::any(). This means it will respond to "get" and "post" requests.
However I would recommend splitting your route to be getSearch() and postSearch() functions and do this:
Route::get('/hostesses/search', array(['as' => 'hostesses.getsearch', 'uses' => 'HostessesController#getSearch');
Route::post('/hostesses/search', array(['as' => 'hostesses.postsearch', 'uses' => 'HostessesController#postSearch');
Route::resource('hostesses', 'HostessesController');
public function getSearch()
{
return View::make('hostesses.search');
}
public function postSearch()
{
// Do validation on form
// Get search results and display
}
And update your form
{{ Form::open(array('route' => 'hostesses.postsearch', 'class' => 'navbar-form navbar-right')) }}
You need to define a POST route:
Route::post('/hostesses/postSearch',array('as'=>'hostesses.search','uses' => 'HostessesController#postSearch'));
Then in your controller
public function postSearch()
{
var_dump(Input::get('search_term'));
}
Related
I want to create a form so when I click submit it goes to root url with post method. I created following
{{ Form::open(array('route' => array('send'), 'method' => 'post')) }}
...
{{ Form::close() }}
and in routes
Route::get('/', 'Controller#home');
Route::post('/', 'Controller#home')->name('send');
But the $request value from controller is null and the method is get instead of post.
No need to use method=>post with route, it is used with url read in docs. Change your Form::open like as below
Either
{{ Form::open(['route' => 'send']) }}
Or
{{ Form::open( ['url' => '/','method' => 'post'] ) }}
You have two route with identical beginnings ("\") pointing to the same controller function.
Try replacing the second one with something like
Route::post('/home', 'Controller#homepost')->name('send');
Just duplicate your home method and make sure you have something like:
public function homepost (Request $request)
{ .... }
You can use Route::any() If you want same url or function to response with multiple type requests like get, post etc.
Route::any('/', 'Controller#home');
Or you could use Route::match().
Route::match(['get', 'post'], '/', Controller#home');
I'm working on a website that searches a database of organizations. On the search page, there are two search fields: one for searching by record name, and one searching the organization's subjects.
Now, normally, I would have no problem setting up placeholders in my URIs.
Route::get('/search/{name}', function($name)
{
//code...
});
And I use the post route to attach the parameters
Route::post('/search', array( 'as' => 'results', function()
{
$string = Input::get('search');
return Redirect::to("/search/$string");
}));
And the Laravel form would have no problem...
<h4>Search by Name</h4>
{{ Form::open(array('url' => "search") )}}
<p>
<div class="input-group input-group-lg">
<span class="input-group-btn">
{{ Form::button('Search', array('class' => 'btn btn-default', 'type' => 'submit'))}}
</span>
{{ Form::text('search', '', array('class' => 'form-control', 'placeholder' => 'Search by name')) }}
</div>
</p>
{{ Form::close() }}
But how do I attach a query string to this part?
{{ Form::open(array('url' => "search") )}}
How I would like my code to behave is when a query string is present, it searches by subject, not name. Doing this:
{{ Form::open(array('url' => "search/?subject=true") )}}
Doesn't actually attach it to my url.
The one thing I could do is just have a hidden input that tells the code to search by subject and not name, but that would mean any users who go to the url again will get different results. I don't want that behavior.
Any help? Laravel documentation doesn't help and I can't seem to find anyone online with the same problem.
[edit]
I found the trick to putting it into the url is by attaching the query string on the Route::post() like so:
$string = Input::get('search');
$subject = Input::get('subject');
return Redirect::to("/search/$string?subject=$subject");
But then Laravel gives me a NotFoundHttpException even after I change the final route to
Route::get('/search/{name}?subject={subject}', function($name, $subject)
Try
Route::get('/search',function($name)
{
//code...
if(Input::has('subject'))
$subject = Input::get('subject');
...
});
Laravel Router system already adds all of your non-route parameters as queries, so if you have a router:
Route::get('/search/{name?}', ['as' => 'search', function($name)
{
//code...
}]);
And you do
return Redirect::route('search', ['name' => 'laracon', 'subject' => 'true']);
It will redirect to:
http://server/search/laracon?subject=true
And
return Redirect::route('search', ['subject' => 'true']);
To
http://server/search?subject=true
And, of course, inside your router you have access to both of them:
Route::get('/search/{name?}', ['as' => 'search', function($name)
{
var_dump(Input::get('name'));
var_dump(Input::get('search'));
}]);
I am trying to insert some data in database. I have created form with all fields and controller.
CarController.php
public function create() {
// load the create form (app/views/pages/create.blade.php)
return View::make('pages.create');
}
public function store() {
// store
$data = new Car;
$data->id = Input::get('id');
$data->save();
// redirect
Session::flash('message', 'Successfully created data!');
return Redirect::to('pages/cars');
}
routes.php
Route::resource('cars', 'CarController');
Route::post('desc', array('uses' => 'CarController#show'));
Route::post('create', array('uses' => 'CarController#create'));
Route::post('store', array('store' => 'CarController#store'));
create.blade.php
{{ Form::open(array('url' => 'store', 'class'=>'form-horizontal')) }}
<div class="form-group">
{{ Form::label('id', 'Vehicle ID',array('class'=>'control-label col-lg-4')) }}
<div class="col-lg-8">
{{ Form::text('id', Input::old('textId'), array('class' => 'form-control', 'placeholder'=>'Vehicle ID')) }}
</div>
</div>
{{ Form::submit('Create the Car!', array('class' => 'btn btn-primary')) }}
</div>
The problem is that it shows the following error:
expects parameter 1 to be a valid callback, no array or string given
I dont know what I have done wrong because I am new at laravel
You are using a resource controller and that's why you only need one route declaretion for this, like:
Route::resource('cars', 'CarController');
You have also declared following routes:
Route::post('desc', array('uses' => 'CarController#show'));
Route::post('create', array('uses' => 'CarController#create'));
Route::post('store', array('store' => 'CarController#store'));
Actually you don't need these route declarations, only use first route and this will create routes for your resource controller. For example, your routes would look like these:
Method | Url | Action | Route Name
--------------------------------------------
GET | /cars/create | create | cars.create // domain.com/cars/create using GET
POST | /cars | store | cars.store // domain.com/cars using POST
There are more, you should check the Resource Controllers. In this case, you have two methods in your controller and if you use a url like yourdomain.com/cars/create using GET method (if you navigate from browser's address bar) then it'll invoke create method and if you submit a form using POST method to yourdomain.com/cars then it'll invoke the store method and all your form fields will be available in the $_POST array and you can use Input::get('id') to get id field's value. Check the socumentation for more information.
change this line in the route.php file
Route::post('store', array('store' => 'CarController#store'));
to
Route::post('uses', array('uses' => 'CarController#store'));
'uses' specify which function to call when you access the route
I'm having a hard time setting up simple links/actions.
In my index view, I have this little form that I want to launch the getTest action in the ProjectsController when I click on the button:
{{ Form::open(array('action' => array('ProjectsController#getTest', $project->id))) }}
<button type="submit"><i class="icon-arrow-up"></i></button>
{{ Form::close() }}
This is the getTest function :
public function getTest(){
echo "test";
return 'test';
}
But this keeps getting me a "Array_combine(): Both parameters should have an equal number of elements" error.
I tried making this work with a route. with this form open instead :
{{ Form::open(['method' => 'GET', 'route' => ['test_route', $project->id]]) }}
And this route :
Route::get('projects/test', array('as' => 'test_route', 'uses' =>'ProjectsController#getTest'));
But I still have the same error.
I can't find any good doc on routing/sending to actions that don't give me this problem. I don't see what
Your route doesn't need parameter, so I think this code is sufficient:
{{ Form::open(['method' => 'GET', 'route' => 'test_route']) }}
I believe the problem is you are adding parameters to the action, but you are not managing those parameters in your routes, nor is your getTest() function accepting any parameters. Another problem is you are setting your route as a GET route, but your form is going to be using POST.
It would be much easier instead on your form to use Form::hidden('id', $project->id); And then in your getTest() function, you could get the variable using $id = Input::get('id');. You'd also be able to use your route name in your form as well. Form::open(array('route'=> 'test_route', method=> 'get'));
I am building a practice app with the Laravel framework I built a form in one of the views which is set to post to the same view itself but when I hit submit the form is posted however I do not get the desired output, I see the original view again.
Here is my view index.blade.php
#extends('master')
#section('container')
<div class="wrapper">
{{ Form::open(array('url' => '/', 'method' => 'post')) }}
{{ Form::text('url') }}
{{ Form::text('valid') }}
{{ Form::submit('shorten') }}
{{ Form::close() }}
</div><!-- /wrapper -->
#stop
and my routes.php
Route::get('/', function()
{
return View::make('index');
});
Route::post('/', function()
{
return 'successfull';
});
What I've tried so far
I tried changing the post to a different view and it worked.
However I want the form to post to the same view itself.
Instead of returning a string I tried to return make a view still it
didn't work.
What am I doing wrong?
addendum
I see that when the form is making the post request I am getting a 301 MOVED PERMANENTLY HEADER
{{ Form::open(array('url' => ' ', 'method' => 'post')) }}
Passing a space as the url has worked for me.
I think this post: Form submits as GET Laravel 4 is related to your problem. I think the problem as I undersood it is caused by end a form url with a / . I found this when having problems to using post to a ./ url in my form. There is also a bug at github that seems like it is related https://github.com/laravel/framework/issues/1804.
I know this is an old question but I found this thread having the same problem so hopefully someone else is helped by my answer.
You need to make sure that your form's method does NOT end in a / for it to be routed correctly. For example if you have the following route:
Route::post('form/process', function()
{
# code here ...
});
Then you need to have the following form definition:
<form action="/form/process" method="POST">
I hope that helps.
I have same problem with OSx + MAMP, initially I've resolved with Raul's solution:
{{ Form::open(array('url' => ' ', 'method' => 'post')) }}
but after consultation with my friend we have concluded that my problem was due to the fact
my lavarel project is avaliable by long local path, as:
http://localhost/custom/custom2/...
in this location the post/get method on root path ("/") not working correctly.
Lavarel to working correctly must be avaliable by "vhost", in this case the problem get/post method on root location "/" not exist.
My friend advised me to use http://www.vagrantup.com/
BYE
There is some helpfull information in the Laravel Docs. Check these out:
Resource Controllers (or RESTful Controllers)
Forms & HTML
Opening A Form
Routing
Named Routes
I recommend you read the Resource Controllers documentation as it makes form handling a lot easier.
Well, you just return the view, so nothing change. You should bind your route to a controller to do some logic and add data to your view, like this:
index.blade.php
#extends('master')
#section('container')
<div class="wrapper">
#if (isset($message))
<p>{{$message}}</p>
#endif
{{ Form::open(array('url' => '/', 'method' => 'post')) }}
{{ Form::text('url') }}
{{ Form::text('valid') }}
{{ Form::submit('shorten') }}
{{ Form::close() }}
</div><!-- /wrapper -->
#stop
Your routes
Routes::any('/', 'home#index');
You controller HomeController.php
public function index()
{
$data = array();
$url = Input::get('url');
if ($url)
$data['message'] = "foo";
return View::make('index', $data);
}
You can also modify your current routes without using a controller like this (use the new view file)
Route::get('/', function()
{
return View::make('index');
});
Route::post('/', function()
{
return View::make('index')->with('message', 'Foo');
});
The problem is with defualt slashed in Apache from 2.0.51 and heigher:
http://httpd.apache.org/docs/2.2/mod/mod_dir.html#directoryslash
The best solution if you do not want to change Apache config is to make the post in a different path:
GOOD:
Route::get('/',
['as' => 'wizard', 'uses' => 'WizardController#create']);
Route::post('wizard-post',
['as' => 'wizard_store', 'uses' => 'WizardController#store']);
NOT GOOD:
Route::get('/',
['as' => 'wizard', 'uses' => 'WizardController#create']);
Route::post('/',
['as' => 'wizard_store', 'uses' => 'WizardController#store']);