Laravel4 Błąd przy wypłeniainu pól formularza

0

Mam taki problem mam przykład w laravelu, który polega na tym, że jest sobie strona rejestracji i jest routing, który sprawdza czy dane pola zostały poprawnie wypełnione w przypadku kiedy zostały źle wypełnione to wszystko działa dobrze bo pojawia się komunikat, o tym, że by je poprawić natomiast w przypadku kiedy ma nastąpić przekierowanie do właściwej strony wywala błąd w logach apacha nie ma żadnego błędu.

Route::post('registration', array('before' => 'csrf',
function()
{
$rules = array(
'email' => 'required|email|unique:users',
'password' => 'required|same:password_confirm',
'name' => 'required'
);
$validation = Validator::make(Input::all(), $rules);
if ($validation->fails())
{
//return Redirect::to('registration')->withErrors($validation)->withInput();
return "dobrze";

}
$user = new User;
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->name = Input::get('name');
$user->admin = Input::get('admin') ? 1 : 0;
if ($user->save())
{
Auth::loginUsingId($user->id);
return Redirect::to('profile');
}
//return View::make('registration');
//return "źle";
})); 


Route::get('registration', function()
{
return View::make('registration');
});

Route::get('profile', function()
{
//if (Auth::check())
//{
//return 'Witamy! Zostałeś uwierzytelniony!';
//}
//else
//{
return 'Proszę się <a href="login">zalogować</a>';
//}
});

Plik registration.php

 <!DOCTYPE html>
<html>
<head>
<title>Uwierzytelnianie w Laravelu - rejestracja
</title>
<meta charset="utf-8">
</head>
<body>
<h2>Uwierzytelnianie w Laravelu - rejestracja</h2>
<?php $messages = $errors->all('<p style=
"color:red">:message</p>') ?>
<?php foreach ($messages as $msg): ?>
<?= $msg ?>
<?php endforeach; ?>
<?= Form::open() ?>
<?= Form::label('email', 'Adres email: ') ?>
<?= Form::text('email', Input::old('email')) ?>
<br>
<?= Form::label('password', 'Hasło: ') ?>
<?= Form::password('password') ?>
<br>
<?= Form::label('password_confirm',
'Potwierdź hasło: ') ?>
<?= Form::password('password_confirm') ?>
<br>
<?= Form::label('name', 'Nazwa: ') ?>
<?= Form::text('name', Input::old('name')) ?>
<br>
<?= Form::label('admin', 'Administrator?: ') ?>
<?= Form::checkbox('admin','true',
Input::old('admin')) ?>
<br>
<?= Form::submit('Zarejestruj!') ?>
<?= Form::close() ?>
</body>
</html>
0

Po pierwsze i najważniejsze naucz się dzielić kod. Routing nie służy do umieszczania tam całego kodu! Właśnie tak uczą mizerne poradniki a potem jest hejt na laravela jaki to on zły. Połowa kodu który tu napisałeś w routing powinna trafić do kontrolera druga połowa do modelu. W routingu powinno zostać jednie coś takiego.

Route::group( array( 'prefix' => 'registration'), function () {
    Route::get( '/', array( 'as' => 'registration.create', 'uses' => 'ProfileController@create' ) );
    Route::post( '/', array( 'as' => 'registration.store',  'before' => 'csrf', 'uses' => 'ProfileController@store' ) );
} );

Route::get( '/profile', array( 'as' => 'profile', 'uses' => 'ProfileController@show' ) );

i jak tak bedziesz miał to możesz użyć, a przynamniej zakładam, że to ta linjka ci nie chodziła.

return Redirect::route('profile');
0

Ten efekt co dałeś nie zadziałał.
Teraz jak się z tym bawię to zauważyłem, że dam taki kod

 Route::post('registration', 
function()
{
$rules = array(
'email' => 'required|email|unique:users',
'password' => 'required|same:password_confirm',
'name' => 'required'
);
$validation = Validator::make(Input::all(), $rules);
if ($validation->fails())
{
//return Redirect::to('registration')->withErrors($validation)->withInput();
return "dobrze";

}

});

I w momencie kiedy formularz jest źle wypełniony to drukuje napis dobrze, ale w momencie kiedy jest dobrze to nie ma nawet pustej strony.

0

W Laravelu 5 to taki formularz rejestracji się generuje artisanem. Ale co do L4 (nie wiem dlaczego ma być odradzany???), po kolei;

app/routes.php

Route::get('register', 'UserController@getRegister');
Route::post('register', 'UserController@postRegister'); 

app/models/User.php

<?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

	use UserTrait, RemindableTrait;

	protected $table = 'users';
	protected $hidden = array('password', 'remember_token');

        public static $rules = array(
            'username'=>'required|alpha|min:2',
            'password'=>'required|alpha_num|between:6,12|confirmed',
            'password_confirmation'=>'required|alpha_num|between:6,12'
        );
}

app/controllers/UserController.php

<?php

class UserController extends BaseController
{

    public function getRegister()
    {
        return View::make('user.registerForm');
    }

    public function postRegister()
    {
        $validator = Validator::make(Input::all(), User::$rules);

        if ($validator->passes())
        {
            $user = new User;
            $user->username = Input::get('username');
            $user->password = Hash::make(Input::get('password'));
            $user->save();

            return Redirect::to('login')->with('message', 'Dziękujemy za rejestrację');
        } else
        {
            // wyświetlanie błędów
        }
    }
}

app/views/user/registerForm.php

<!DOCTYPE HTML>
<html>
    <head>
        <title>Rejestracja</title>
        <meta charset="UTF-8" />
    </head>
    <body>
       <form method="POST" action="/register" accept-charset="UTF-8" class="box register"> 
        <fieldset class="boxBody">
            <label>Username</label>
            <input type="text" tabindex="1" name="username" required>
            <label>Password</label>
            <input type="password" name="password" tabindex="2" required>
            <label>Confirm password</label>
            <input type="password" name="password_confirmation" tabindex="2" required>
        </fieldset>
        <footer>
            <input type="submit" class="btnLogin" value="Register" tabindex="4">
        </footer>
    </form>
</body>
</html>
1

Akurat (no w sumie nie całkowicie) polska dokumentacja dotyczy L4.2:
http://laravel-docs.pl/

Przetłumaczone ale tylko częściowo. Tu chyba nie ma większych przeszkód żeby podstawy ćwiczyć nawet na 4.2 a potem przejść na 5.3.

1 użytkowników online, w tym zalogowanych: 0, gości: 1