laravel 4-call_user_func_array() - php

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

Related

Laravel root path post method

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

Laravel route not defined

I'm trying to send a contact form with Laravel
So in the top of my contact form I have this
{{ Form::open(['action' => 'contact', 'name'=>"sentMessage", 'id'=>"contactForm"])}}
I have routes for contact page like this
Route::get('/contact', 'PagesController#contact');
Route::post('/contact','EmailController#test');
in my EmailController file I have something like this
public function test()
{
return View::make('thanks-for-contact');
}
Whenever I open my contact page I get this error message
Route [contact] not defined
when you use the attribute action you provide it a method in your controller like so :
// an example from Laravel's manual
Form::open(array('action' => 'Controller#method'))
maybe a better solution with be to use named routes, which will save you a lot of time if you ever wanted to change your URL.
Route::get('/contact', array('as' => 'contact.index', 'uses' => 'PagesController#contact'));
Route::post('/contact', array('as' => 'contact.send', 'uses' => 'EmailController#test'));
then your form will look something like this :
{{ Form::open(array('route' => 'contact.send', 'name'=>"sentMessage", 'id'=>"contactForm")) }}
You are using 'action' in your opening tags, so its trying to go to a controller by that name. Try using 'url' => 'contact'.

Laravel 4 - NotFoundHttpException only on one custom route

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

Laravel 4 - Redirect with flash message not working

In my Laravel 4 project, I am trying to set a flash message when redirecting, like so:
return Redirect::route('main')->with('flash', 'Message here.');
(I looked at the following: Laravel 4 - Redirect::route with parameters).
The redirect happens properly. In my 'main' view I've got this code to display the flash message:
#if (Session::has('flash'))
<div class="flash alert">
<p>{{ Session::get('flash') }}</p>
</div>
#endif
But it doesn't work: Session::get('flash') returns an array, not a string. If I insert a call to var_dump(Session::get('flash')), I get this output:
array (size=2)
'new' =>
array (size=0)
empty
'old' =>
array (size=1)
0 => string 'flash' (length=5)
What is happening? It seems like fairly straightforward code...
When you redirect with data, the data you save will be 'flashed' into session (meaning it will only be available during the next request, see: http://laravel.com/docs/session#flash-data).
I would be wary of using 'flash' as a flash data key, as Laravel stores its flash data in a key already with a name of 'flash'. Try changing your name to something else. You may be confusing Laravel's Session::get as to where it should be attempting to load the data from.
Inside of the routes.php file try to create your routes within the
Route::group(['middleware' => ['web']], function () {
Route::get('/', [
'as' => 'home',
'uses' => 'PagesController#home'
]);
Route::resource('tasks', 'TasksController');
});
And After that use.
#if (Session::has('flash'))
<div class="flash alert">
<p>{{ Session::get('flash') }}</p>
</div>
#endif
Where you need to access session

laravel form post issue

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

Categories