Laraver routing error - php

I have weird route error in Laravel. Stucked fixing it.
Here is my action (it is in simplified version).
public function ibf() {
$existing_board_id = Input::get('existing_board_id');
...
return Redirect::route('board.index')->with('success', 'Board is successfully imported.');
}
Form, here $board is new Model, not saved one, but with pre-defined values.
{{ Form::model($board, ['route' => 'board.ibf', 'method' => 'POST', 'class' => 'form-horizontal']) }}
routes.php
Route::group(array('prefix' => 'board'), function() {
Route::post('/ibf/', array(
'as' => 'board.ibf',
'uses' => 'BoardController#ibf'
));}
Error:
protected function methodNotAllowed(array $others)
{
throw new MethodNotAllowedHttpException($others);
}
Everything looks simple, but I really dont understand the problem.

Related

Laravel routing and CSRF protection

If I have this line of code in my routes.php file:
Route::when('*', 'csrf', array('post', 'put', 'patch', 'delete'));
Do I still need to do this?
Route::group(array('before' => 'csrf'), function() {
Route::post('/search', array(
'as' => 'search-post',
'uses' => 'SearchController#postSearch'
));
});
Or is it ok to just do this?
Route::post('/search', array(
'as' => 'search-post',
'uses' => 'SearchController#postSearch'
));
Route::when filters (internally called pattern filters) are called right before before filters. You're all good with just using your routes normally.
Here's the relevant source code:
public function callRouteBefore($route, $request)
{
$response = $this->callPatternFilters($route, $request);
return $response ?: $this->callAttachedBefores($route, $request);
}
As you can see first the pattern filters will be called. If they return any response it will be returned from here, otherwise the "normal" before filters will be called.
Yes; you should be safe with just Route::post('/search', [...]);.

Laravel 4 form post issue

my problem is that when i submit my form it pass through the get method.
The workflow is form submit -> get -> post and it should be form submit -> post.
I need to have a condition in my get method to validate de array is not null
My code:
Routes
Route::get('/pre-register-birth',array('as'=>'pre-register-birth', 'uses'=>'UserController#preRegisterBirthData'));
Route::post('/pre-register-birth', 'UserController#preRegisterBirthDataPost');
View
{{ Form::open(array('method' => 'POST', 'action' => 'UserController#preRegisterBirthDataPost',
'class'=>'form-horizontal', 'id'=>'regist-form')) }}
Controller
public function preRegisterBirthData()
{
$user = Session::get('user');
if ($user)
return View::make('user/pre-register-birth')->with('tempUser', $user);
else
return Redirect::Route('pre-register-get');
}
public function preRegisterBirthDataPost()
{
$validator = Validator::make(Input::all(),
array(
'birthplace' => 'required|max:100',
'birthdate' => 'required|max:100|date|date_format:Y-m-d'
)
);
if ($validator->fails()) {
return Redirect::Route('pre-register-birth')
->withErrors($validator)
->withInput();
} else {
$user = array(
'email' => Input::get('email'),
'pass' => Input::get('pass'),
'name' => Input::get('name'),
'surname' => Input::get('surname'),
'birthplace' => Input::get('birthplace'),
'birthdate' => Input::get('birthdate'),
'hourKnow' => Input::get('hourKnow'),
'dataCorrect' => Input::get('dataCorrect'),
'news' => Input::get('news'),
);
return Redirect::Route('pre-register-terms')->with('user', $user);
}
}
I think I've seen this issue before. Laravel for whatever odd reason doesn't like the action => ... So, change your form declaration from a to b:
// A
{{ Form::open(array('method' => 'POST', 'action' => 'UserController#preRegisterBirthDataPost',
'class'=>'form-horizontal', 'id'=>'regist-form')) }}
// B
{{ Form::open(array('method' => 'POST', 'url' => 'pre-register-birth',
'class'=>'form-horizontal', 'id'=>'regist-form')) }}
Notice I changed action => ... to url => ... It's a small change, but it might solve it. However, you may need to add in:
Route::post('/pre-register-birth', array('as'=> 'pre-register-birth', 'uses' => 'UserController#preRegisterBirthDataPost'));
So it recognizes the named route.
Hope this helps!

laravel route doesn't exist though it exits

This is my route:
Route::post('admins.login', 'AdminsController#login');
This is my form
{{ Form::open(array('route' => 'admins.login', 'class' => 'loginClass', 'method' => 'post')) }}
This is the exception:
Route [admins.login] not defined.
The method:
public function login(){
echo "Save Time";exit;
}
Edit
I already tried making / instead of . in all situations
Route definition for a named route:
Route::post('admins/login', array('uses' => 'AdminsController#login', 'as' => 'admins.login'));
Form:
{{ Form::open(array('route' => 'admins.login', 'class' => 'loginClass')) }}

ZfcUser and custom showAction()

I've got a problem. I'm using zfcuser in my project, and now i want to create profile pages. I'have created the showAction() in UserController, but when i put the url to webbrowser, i will got 404.
public function showAction()
{
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute('zfcuser', array(
'action' => 'register'
));
}
try {
$user = $this->zfcUserAuthentication()->getIdentity()->getUser($id);
}
catch (\Exception $ex) {
return $this->redirect()->toRoute('zfcuser', array(
'action' => 'register'
));
}
return new ViewModel(array(
'user' => $user));
}
I also created custom function to get User info all in one row.
It looks like this:
public function getUser($id){
$rowset = $this->tableGateway->select(array('user_id' => $id));
$row = $rowset->current();
return $row;
}
And it is in Entity/User.php
This is my showAction() in zfc UserController.php
I thought its wrong module.config, but its not working anyway. I changed my module.config to this:
'show' => array(
'type' => 'Literal',
'options' => array(
'route' => '/show[/][:id]',
'defaults' => array(
'controller' => 'zfcuser',
'action' => 'show',
),
),
),
My problem is, i can not even get to url to show this controller action.
Where can be problem. I want to make simple user profile page.
The problem is in my opinion your route 'Literal'. A literal route can't have params.
You have to use Segment instead
But it's not the only problem i think.
ZfcUser comes with a packaging, profile page already exist :
Take a look to this complete slideshare :
Zf2 Zfc-User

Laravel 4 Form passing 2 parameters

Currently i'm working on a project to manage my cost on fuel.
Now i try to pass 2 parameters in a Form::open() which sadly doesn't work.
The reason why i think i need to pass 2 parameters at once is because my url is Sitename/car/{id}/tank/{id}
What am i doing wrong?
edit.blade.php
Form::open(array('class' => 'form-horizontal', 'method' => 'put', 'action' => array('TankController#update', array($aid, $id))))
Problem Code
'action' => array('TankController#update', array($aid, $id)
-Results in the following error:
Parameter "tank" for route "car.{id}.tank.update" must match "[^/]++" ("" given) to generate a corresponding URL.
TankController.php
public function edit($id, $tid)
{
$tank = Tank::find($tid);
if(!$tank) return Redirect::action('TankController#index');
return View::make('Tank.edit', $tank)->with('aid', $id);
}
public function update($id, $tid)
{
$validation = Validator::make(Input::all(), Tank::$rules);
if($validation->passes()){
$tank = Tank::find($tid);
$tank->kmstand = Input::get('kmstand');
$tank->volume = Input::get('volume');
$tank->prijstankbeurt = Input::get('prijstankbeurt');
$tank->datumtank = Input::get('datumtank');
$tank->save();
return Redirect::action('TankController#index', $id)->with('success', 'Tankbeurt succesvol aangepast');
} else return Redirect::action('TankController#edit', $id)->withErrors($validation);
}
Route.php
Route::resource('car', 'CarController');
Route::resource('car/{id}/tank', 'TankController');
Route::controller('/', 'UserController');
-Url Structure
SITENAME/car/2/tank/2/edit
I've also looked into the api documents but found nothing.
http://laravel.com/api/source-class-Illuminate.Html.FormBuilder.html
Thanks in advance
Try this:
Form::open(array('class' => 'form-horizontal', 'method' => 'put', 'action' => array('TankController#update', $aid, $id)))
This is the best solution
Form::open (array ('method'=>'put', 'route'=>array($routeName [,params...]))
An example:
{{ Form::open(array('route' => array('admin.myroute', $object->id))) }}
https://github.com/laravel/framework/issues/491
Try to use in your blade:
For Laravel 5.1
{!! Form::open([
'route' => ['car.tank.update', $car->id, $tank->id],
'method' => 'put',
'class' => 'form-horizontal'
])
!!}

Categories