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'.
Related
I have the following in my Routes.php:
Route::get('cat/{cat}', ['as' => 'cat', 'uses' => 'CatController#get']);
I want to check in my sidebar.blade.php file if any of the views returned from the Controller function matches the current page.
{cat} could be either a,b,c,d,f or e.
The sidebar consists of 6 images.
If for example the route is cat/a the image of tis route should be changed.
People suggested Route::current()->getName() but this only returns cat and not /a, /b, /c, etc. Also some other functions are only returning cat/ and nothing after that
You can use Request::is('cat/a').
You can get {cat} part with this:
$cat = Request::route()->getParameter('cat');
And the route with:
$route = Route::currentRouteName();
In your routes/web.php:
Route::get('cat/{cat}', ['as' => 'cat', 'uses' => 'CatController#get'])->name('name-your-route');
In view.blade.php:
#if(request()->routeIS('name-your-route'))
#endif
After hours of searching I still could not find my answer regarding L5.
What my issue is :
I want to make a link something like this:
localhost:800/songs/you-drive-me-crazy
BUT what is get is:
localhost:800/songs?you-drive-me-crazy
my route parameter is changing into query string.
//routes.php
$router->bind('songs', function($slug)
{
return App\Song::where('slug', $slug)->first();
});
$router->get('songs', ['as' => 'songs.index', 'uses' => 'SongsController#index'] );
$router->get('songs/{songs}', ['as' => 'songs.show', 'uses' => 'SongsController#show'] );
I am using:
{!! link_to_route('songs.index', $song->title, [$song->slug]) !!}
I have tried everything but not succeeded yet,your suggestion may be helpful.
Thanks.
Your usage of link_to_route is incorrect:
{!! link_to_route('songs.index', [$song->title, $song->slug]) !!}
The first parameter is the route name, the second parameter is an array of route parameters, preferably using key value. Because you did not show your defined route, it's hard to guess what this associative array should look like:
{!! link_to_route('songs.index', ['title'=>$song->title, 'slug'=>$song->slug]) !!}
Also I advise you to use the documented functions: route(), see: http://laravel.com/docs/5.0/helpers#urls
A correctly requested route using route():
{!! route('songs.index', ['title'=>$song->title, 'slug'=>$song->slug]) !!}
A properly formatted route would then be:
Route::get('songs/{title}/{slug}', ['as' => 'songs.index', 'uses' => 'SomeController#index']);
This will result in a URL like: http://localhost:800/songs/you-drive-me-crazy/slug
If you only want to add the title to the URL but not the slug, use a route like this:
Route::get('songs/{title}', ['as' => 'songs.index', 'uses' => 'SomeController#index']);
This will result in a URL like: http://localhost:800/songs/you-drive-me-crazy/?slug=slug
Using
Route::get('songs/{slug}', ['as' => 'songs.index', 'uses' => 'SomeController#index']);
The URL will be like: http://localhost:800/songs/you-drive-me-crazy/?title=title assuming the slug now is you-drive-me-crazy
Any added parameter in a route() call will be added as a GET parameter if it's not existing in the route definition.
fixed it, thanks for your great concerns and suggestions.
I was linking to wrong route here:
`{!! link_to_route('songs.index', $song->title, [$song->slug]) !!}`
now, I changed it as :
`{!! link_to_route('songs.show', $song->title, [$song->slug]) !!}`
and it did the trick.
I have defined route in Route File as :
Route::get('user/update','Users#Update');
I want to fill my model data to form so i am writing form::model
<?php echo Form::model($users,array('route' => array('user.update', $users->id))) ?>
It show me error :
Route [user.update] not defined.
If i write
<?php echo Form::model($users) ?>
Then it is working perfectly.
The default method created by the Form class is "POST", so you need:
1) to name the route (as correctly pointed out by #Joel);
2) to make it answer to the proper HTTP verb:
Route::post('user/{id}/update',['as' => 'user.update', 'uses' => 'Users#Update']);
If you're using it for both GET and POST, use the any method:
Route::any('user/{id}/update',['as' => 'user.update', 'uses' => 'Users#Update']);
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'));
}
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