How could i hide the parameters of a get route in laravel 5?
I mean, a route can have required parameters, and also optional parameters, i would like to know how to hide that parameters.
Here's Laravel docs for Route parameters
You can capture segments of the request URI within your route:
Route::get('user/{id}', function($id)
{
return 'User '.$id;
});
If my domain is: example.com, when i access to example.com/user/201348 i would like that in the browser the URL be: example.com/user for example.
What you need is not a get route but a post route.
Route::get('user/', function(Request $request)
{
return 'User '.$request->get('id');
});
But keep in mind:
You need to create a form to generate a post request.
{{ Form::open(array('url' => 'user')) }}
{{ Form::hidden('id', $userId); }}
{{ Form::submit('Show user with id '.$userId); }}
{{ Form::close() }}
Related
I have the below form in a larvel view:
<div id="admin">
<h1>Products Admin Panel</h1><hr>
<p>Here you can view, delete, and create new products.</p>
<h2>Products</h2><hr>
<!--admin/fileupload/create-->
{{ Form::open(array('url'=>'admin/products/create' , 'files'=>true)) }}
<p>
{{ Form::file('image') }}
</p>
{{ Form::submit('Create Product' , array('class'=>'secondary-cart-btn')) }}
{{ Form::close() }}
</div> <!-- end admin -->
I am new to laravel and basically just want to understand , in the URL when i specify 'url'=>'admin/products/create' , what is laravel going to look for ?
a modal called products ? or a controller called products ? and a method getCreate or postCreate inside it ? what is admin then , i want to understand how laravel interprets this blade form url , can anybody explain ?
I want somebody to explain to me how does laravel interpret blade form url ?
In laravel,you will specify which url to be processed by which controller and by which method inside that controller.This specify must be do in routes.php file that is located in projectname/app/Http/routes.php .
When you specify the 'url'=>'admin/products/create' you must define the route in routes.php .
Route can be define in different ways like:
Route::get('admin/products/create','ProductController#crete');
Here you can use get or post according to your request.
Another way you can do is
Route::get('admin/products/create',array(
'as'=> 'create-product',
'uses'=>'ProductController#create'
));
Now , you can do like this route('create-product'); instead of 'url'=>'admin/products/create' .
Another way by using Route group
Route::group(['prefix'=>'admin'],function(){
Route::group(['prefix'=>'products'],function(){
Route::get('/create',array(
'as'=>'create-product',
'uses'=> 'ProductController#create'
));
// Here you can define other route that have the url like /admin/products/*
});
});
Now you can do like route('create-product') or 'url'=>'admin/products/create' Advantage
For more info check the documentation Here
Using 4.2 and trying to add a custom method to my controller.
My routes are:
Route::get('ticket/close_ticket/{id}', 'TicketController#close_ticket');
Route::resource('ticket', 'TicketController');
Everything CRUD wise works as it should, but at the bottom of my TicketController I have this basic function:
public function close_ticket($id) {
return "saved - closed";
}
When I am showing a link to route on my page:
{{ link_to_route('ticket/close_ticket/'.$ticket->id, 'Mark As Closed', array($ticket->id), array('class' => 'btn btn-success')) }}
I constantly get a route not defined error, but it surely is defined...?
Any ideas where this is going wrong?
link_to_route expects a route name, not a url. This is why you are getting 'route not defined' errors, because you have not defined a route with the name you supplied to link_to_route. If you give your route a name, you can use link_to_route.
Given the following route definition, the name of the route is now 'close_ticket':
Route::get('ticket/close_ticket/{id}', array('as' => 'close_ticket', 'uses' => 'TicketController#close_ticket'));
The value for the 'as' key is the route name. This is the value to use in link_to_route:
{{ link_to_route('close_ticket', 'Mark As Closed', array($ticket->id), array('class' => 'btn btn-success')) }}
The laravel helper method link_to_route generates an HTML link. Which mean when clicked, the user will be performing a GET request.
In your routes file, you are defining this as a POST route.
Route::post(...)
Also, take a look at the docs for link_to_route here:
http://laravel.com/docs/4.2/helpers
You'll see that the first argument should be just the route name, without the ID appended.
I created a very simple form so that I could use a submit button rather than a link to open up an edit users page. Using a link works perfectly, but the form button fails and yields a MethodNotAllowedHttpException even though the method ("edit") is perfectly defined in the UsersController resource and otherwise works fine.
Route:
Route::resource('users','UsersController');
UsersController:
public function edit($id)
{
$user = $this->user->find($id);
return View::make('users.edit')->with('user',$user);
}
show.blade.php:
<!-- This works fine: -->
{{ link_to_route('users.edit', ("Edit: " .$user->first_name." ".$user->last_name), $user->id) }}
<!-- This doesn't work, and yields the Method Not Allowed exception: -->
{{ Form::open(array('route' => array('users.edit',$user->id))) }}
{{ Form::submit('Edit User', array('class'=>'button')) }}
{{ Form::close() }}
Thanks.
When you do Form::open(), it defaults to using the post request method. But when you create a Route::resource(), the edit method takes a get request.
To make it work through the form, you'll need to open it with an additional parameter, like this:
{{ Form::open(array('route' => array('users.edit',$user->id),
'method' => 'get')) }}
You need to point to the update route, not edit.
{{ Form::open(array('route' => array('users.update', $user->id))) }}
The edit route is for displaying the view, while the update is for the put/patch request.
For more information about using the RESTful routes, I'd recommend checking out http://laravel.com/docs/controllers#resource-controllers
I'm trying to update a column in my database with a total of votes on a project. I'm having a 404 problem but I don't see the problem in my route. (PS: Laravel 3)
This is my Vote button on every project :
{{ Form::open('project/addvote', 'VOTE') }}
{{ Form::hidden('id', $project->id) }}
{{ Form::submit('Vote') }}
{{ Form::close() }}
So when you click on the vote button it use this route :
Route::put('project/addvote', array('uses'=>'projects#addvote'));
And this is my action in the projects controller (no updating yet, just trying to redirect) :
public function put_addvote(){
return Redirect::to_route('project', $id)
->with('message', 'Vote successful');
}
Redirecting to this route :
Route::get('project/(:num)', array('as'=>'project', 'uses'=>'projects#project'));
And this is getting me the 404 error
Thanks to every response and the great help here!
Actually, Redirect::to_route expects a name of a route to redirect to it and a named route has to be declare with a name, like,
Route::put('project/addvote', array('as' => 'project', 'uses'=>'projects#addvote'));
So, you can use it;s name to redirect to it like
return Redirect::to_route('project');
Here, project has been used as it's name using 'as' => 'project'. In, your example, you didn't gave any name to the route, here
Route::put('project/addvote', array('uses'=>'projects#addvote'));
The, as => 'route_name' is missing.
For second question, you can do it as
$id = Input::get('id');
Project::find($id);
Project->votenumber = 5;
Project->->save();
Inserting & Updating Models.
Update :
It was a bit confusing but after a conversation through the commenting system the answer for routing is give below :
You (OP) mentioned that you have a route declared as
Route::get('projets', array('as'=>'projets', 'uses'=>'projets#index'));
To this route you are trying to redirect using this
return Redirect::to_route('project', $id);
So, you are passing a parameter and it's not in your route declaration and this is the problem, so to overcome this change your route declaration to this
Route::get('projets/(:num)', array('as'=>'projets', 'uses'=>'projets#index'));
Or
Route::get('projets/(:any)', array('as'=>'projets', 'uses'=>'projets#index'));
Or, you can make the param optional using a ?, for example :
Route::get('projets/(:any?)', array('as'=>'projets', 'uses'=>'projets#index'));
Check wildcard Routes.
Update :
You should have postd the original code with the question, anyways, Change this
return Redirect::to_route('project', $id);
to
return Redirect::to_route('project', array($id));
I'm having some strange behavior with my forms in Laravel 4. I have a "settings" page with two forms, each (are supposed to) POST to a controller method, update the database and return back to the settings page. However, there seems to be an issue, either with the way my forms are working or my routes.
Here's how it is, simplified:
Settings page: (site.com/settings)
<div id="form-one" class="form-area">
{{ Form::open(array('action' => 'SettingController#editOption')) }}
{{ Form::text('optionvalue', 'Default')) }}
{{ Form::submit('Save Changes') }}
{{ Form::close() }}
</div>
<div id="form-two" class="form-area">
{{ Form::open(array('action' => 'SettingController#editPage')) }}
{{ Form::text('pagevalue', 'Default')) }}
{{ Form::submit('Save Changes') }}
{{ Form::close() }}
</div>
So basically, two seperate forms on the same page that post to two seperate methods in the same Controller - when the method is successful, it redirects them back to "settings". I won't post the methods, since tested them and they work, I believe the problem is in the routes file:
routes.php
// Checks if a session is active
Route::group(array('before' => 'require_login'), function()
{
Route::group(array('prefix' => 'settings'), function()
{
Route::get('/', 'SettingController#index');
Route::post('/', 'SettingController#editOption');
Route::post('/', 'SettingController#editPage');
});
});
Now I'm pretty sure it doesn't like the two POST routes being like that, however I cannot think of another way to do it, since the forms are on the same page. I get the error:
Unknown action [SettingController#editOption].
Since the option form comes first I guess. If I take the open form blade code out (for both), it loads the page - but obviously the form doesn't do anything.
Any help would be nice! Thanks in advance.
You can't add two same routes for different actions, because of they will be passed to first matched route and in your case to SettingController#editOption. Change your routes to :
Route::post('/option', 'SettingController#editOption');
Route::post('/page', 'SettingController#editPage');
Than in both actions you can redirect to '/': return Redirect::back(), and if error was occured:
if ($validation->fails())
{
return Redirect::to('/settings')->with_errors($validation);
}
My alternative solution for this is to create an hidden html input in each form and make the controller identify what for is submitted based in this field. So, yu can use just one route for both.