Hello folks I am stuck.
I want to register a User in Laravel 4. Now the thing is, that I want to first grab the email and password and save them in the database. And in step 2 of the registration process, I want to grab all the other details like first and last name and so on.
The difficulty is, that everything should be under one route called signup, for example everything under http://example.org/signup
Another difficulty is, that I have to access the same route with the same methods (GET & POST) twice, because I once get and post the form for Email and Password, and then I get and post the First, Last and Company Name into the Database.
I came up with the following solution, to store everything into the session, because through the session I can access the variables. So whenever I access my UserController I check, if there is data in the session and if yes, redirect to form 2.
Here are all my files:
http://help.laravel.io/d4104cae42f9a2efe1466ce53d086826bc9f6d7f
My Get-Method from the UserController:
public function create()
{
if(Session::has('email')) {
return View::make('frontend.signup.step2');
}
else {
return View::make('frontend.signup.step1');
}
}
My Post-Method from the UserController:
public function store()
{
// If User has a email and password in the session from the first create-View
// his data should be stored and then he gets redirected to a new create-View
Session::flush();
Session::put('email', Input::get('email'));
Session::put('password', Input::get('password'));
if (Session::has('email')) {
try
{
// Let's register a user.
$user = Sentry::register(array(
'email' => Input::get('email'),
'password' => Input::get('password'),
));
// Let's get the activation code
$activationCode = $user->getActivationCode();
// Send activation code to the user so he can activate the account
// Save Email in Emaillist
Email::create(array(
'email' => Session::get('email')
));
// Redirect
return Redirect::action('UserController#create');
}
return Redirect::route('signup');
}
else {
return 'No Session here';
}
}
Here are my routes:
Route::get('signup', array('as' => 'signup', 'uses' => 'UserController#create'));
Route::post('signup', array('as' => 'signup', 'uses' => 'UserController#store'));
For some reason I believe that it gets unneccessary complicated and I believe that there must be another more simple and intuitiv way to solve this, instead with if statements and redirects to the same controller-method.
Nonetheless I came up with some other solutions, for example just using the "signup" as prefix, but I don't like it that way.
Route::group(array('prefix' => 'signup'), function()
{
Route::get('/', function(){
return 'Yeab bababy yea';
});
Route::get('step1', array('as' => 'signup.step1', 'uses' => 'UserController#getStep1'));
Route::post('step1', array('as' => 'signup.step1', 'uses' => 'UserController#postStep1'));
Route::get('step2', array('as' => 'signup.step2', 'uses' => 'UserController#postStep2'));
Route::post('step2', array('as' => 'signup.step2', 'uses' => 'UserController#postStep2'));
});
Is there any way of accomplishing the task while only using one route and without using clientside Javascript to store the variables in the database? (I am a unexperienced with ajax)
The Goal should be to catch the email and still stay on the same route, like those smart guys here for example:
https://www.crazyegg.com/signup
I hope there is a way. Thank you for your help Internet.
Kind regards,
George
P.S.
It's 1 am here in Germany, so don't be mad if I don't respond the next couple of hours to comments, because I am going to sleep now. Thank you very much.
Related
User has logged in, has filled out a survey, then goes to another screen that asks them to tick a box if it is their last survey. The button text changes based on whether the tickbox if ticked or empty.
If the user ticks the box, meaning this is the last survey, I want it to delete that user so they can’t log back in using their user and password data - it also logs them out as they click the button to save and logout. The opposite behaviour is just to logout as normal.
Hoping for any help at all - trying to figure something out at work whilst guys in meetings a lot currently.
public function last()
{
$this->viewBuilder()->setLayout(‘/default’);
$usersData = $this->loadModel('Users');
$auth = $this->Authentication->getResult()->getData();
$deleteUserLoggedIn = $this->Users->find()->where(['Users.code' => $auth->get('code')]);
if(isset($_POST['submit'])) {
if(!empty('name')) {
// delete query here
$deleteUser = $this->Users->delete($deleteUserLoggedIn, ['Users' => 'code']);
return $this->redirect(['controller' => 'Users', 'action' => 'logout']);
} else {
// if not clicked - logout as normal
$this->Flash->success(__('All questions answered, thank you for your participation.'));
return $this->redirect(['controller' => 'Users', 'action' => 'logout']);
}
}
I'm actually inserting data without problems on my DB...but when I try to edit one row and save the changes... it throws me this error...
"No query results for model [model's name]"
I already try to fix that with => protected $primaryKey = 'id'; On my model.
But still getting the same error ....any help ?
The specific error route looks like
"admin/titulares/2"
and before I press the save button like "admin/titulares/2/edit"
My routes
Route::group([
'middleware' => ['prefix' => config('backpack.base.route_prefix', 'admin'),
'web', config('backpack.base.middleware_key', 'admin')],
'namespace' => 'App\Http\Controllers\Admin',
], function () { // custom admin routes
Route::get('dashboard', 'dashboardController#dashboard');
#CRUD::resource('equipos', 'EquiposCrudController');
#CRUD::resource('regiones', 'RegionesCrudController');
CRUD::resource('parametros', 'ParametrosCrudController');
CRUD::resource('estaciones', 'EstacionesCrudController');
#CRUD::resource('redes', 'RedesCrudController');
#CRUD::resource('huso', 'HusoCrudController');
CRUD::resource('titulares', 'TitularesCrudController');
CRUD::resource('operadores', 'OperadoresCrudController');
});
UPDATE:
This is my edit form action (it uses https://laravelcollective.com/docs/5.0/html)
{!! Form::open(array('url' => $crud->route.'/'.$entry->getKey(), 'method' => 'put', 'files'=>$crud->hasUploadFields('update', $entry->getKey()))) !!}
Are you sure using right method for update, You should use put for update data, for example,
Route::put('example/{id}', 'ExampleController#update')
I hope this will help
I need to get the value from link and then add them to DB.
I am working with another developer who created an api as a bridge because he does not know Laravel. Here is the api http://laravel.io/bin/614Xv I am currently passing string valued data like so http://laravel.io/bin/wYry0 and passing the parameters $user, $name, $password through the route.
Here is my route
Route::get('/profile/activated/{user?}/{pass?}/{email?}', array(
'as' => 'invited-user-account-created-get',
'uses' => 'ProfileController#getCreateInvitedUser'
));
Here is my function:
public function getCreateInvitedUser($user=null, $password=null, $email=null) {
$user = Input::get('username', 'abcd');
$pass = Input::get('password', '1234');
$email = Input::get('email', 'asas#gam.com');
//insert $user array in database users table
$user = User::create(array(
'username' => $user,
'password' => $pass,
'email' => $email
));
}
The other developer is wanting me to set the input to the variables I get from the url link/profile/activated/user/pass/email
Maybe I am tired but I am not understanding what he wants me to do and how to grab the variables from the url. The whole reason in doing this is to instantly store the invited guest info in the database when they click the activation link so they do not have to sign up for an account later.
Your route has a problem
Use this instead
Route::get('/profile/activated/{user}/{pass}/{email}', array(
'as' => 'invited-user-account-created-get',
'uses' => 'ProfileController#getCreateInvitedUser'
));
But his idea is totally messed up! Passing password values through a URL, Who does that even?
You sound enlightened, please advise him to try a different approach in whatever thing he's trying to achieve.
I have this defined in my routes.php file
Route::post('gestionAdministrador', array('as' => 'Loguearse', 'uses' => 'AdministradorController#Login'));
Route::post('gestionAdministrador', array('as' => 'RegistrarAdministrador', 'uses' => 'AdministradorController#RegistrarAdministrador'));
And in my login.blade.php file, the form starts as this
{{ Form::open(array('route'=>'Loguearse'))}}
I dont know why when i submit the form takes the second route instead the first one, even though I am pointing to the first one.
There must be a way to go to the same url from two different forms, that is what I want.
If you have two routes with the exact same URI and same method:
Route::post('gestionAdministrador', array('as' => 'Loguearse', 'uses' => 'AdministradorController#Login'));
Route::post('gestionAdministrador', array('as' => 'RegistrarAdministrador', 'uses' => 'AdministradorController#RegistrarAdministrador'));
How can Laravel know the difference between them when something hit /gestionAdministrador?
It will always assume the first one.
The name you set 'as' => 'RegistrarAdministrador' will be used to create URLs based on that route name, only, when something (browser, curl...) hit the URL the only ways to differentiate them is by
1) URL
2) URL parameters (which is basically number 1 plus parameters)
3) Method (GET, POST)
So you could change them to something like:
Route::post('gestionAdministrador/loguearse', array('as' => 'Loguearse', 'uses' => 'AdministradorController#Login'));
Route::post('gestionAdministrador/registrar', array('as' => 'RegistrarAdministrador', 'uses' => 'AdministradorController#RegistrarAdministrador'));
EDIT 2
What you really need to understand is that the name you give to a route ('as' => 'name') will not be part of your url, so this is not something that Laravel can use to differentiate your two URls, this is for internal use only, to identify your routes during the creation of URLs. So, those instructions:
$loguearse = URL::route('Loguearse');
$registrar = URL::route('RegistrarAdministrador');
Would generate exactly the same URL:
http://yourserver.dev/gestionAdministrador
EDIT 1 - TO ANSWER A COMMENT
Redirecting in Laravel is easy, in your controller, after processing your form, in any of your methods you can just:
return Redirect::to('/');
or
return Redirect::route('home');
Having a route like this one:
Route::get('/', array('as' => 'home', 'uses' => 'HomeController#index'));
So, your controller would look like this:
class AdministradorController extends Controller {
public function RegistrarAdministrador()
{
...
return Redirect::route('home');
}
public function Login()
{
...
return Redirect::route('home');
}
}
Actually you have only one route in your route collection, because:
You have following routes declared:
Route::post('gestionAdministrador', array('as' => 'Loguearse', 'uses' => 'AdministradorController#Login'));
Route::post('gestionAdministrador', array('as' => 'RegistrarAdministrador', 'uses' => 'AdministradorController#RegistrarAdministrador'));
Both of these used post method and this is post method:
public function post($uri, $action)
{
return $this->addRoute('POST', $uri, $action);
}
It calls addRoute and here it is:
protected function addRoute($methods, $uri, $action)
{
return $this->routes->add($this->createRoute($methods, $uri, $action));
}
Here $this->routes->add means Illuminate\Routing\RouteCollection::add() and the add() method calls addToCollections() and it is as follows:
protected function addToCollections($route)
{
foreach ($route->methods() as $method)
{
$this->routes[$method][$route->domain().$route->getUri()] = $route;
}
$this->allRoutes[$method.$route->domain().$route->getUri()] = $route;
}
The $routes is an array (protected $routes = array();) and it's obvious that routes are grouped by methods (GET/POST etc) and in each method only one unique URL could be available because it's something like this:
$routes['post']['someUrl'] = 'a route';
$routes['post']['someUrl'] = 'a route';
So, in your case, the last one is replacing the first one and in this case you may use different methods to declare two routes using same URL so it would be in different array, something like this:
$routes['post']['someUrl'] = 'a route';
$routes['put']['someUrl'] = 'a route'; // Route::put(...)
There must be a way to go to the same url from two different forms
Yes, there is a way and it's simply that you have to use the same route as the action of your form and therefore, you don't need to declare it twice.
What you want to do is a bad idea, you shouldn't be logging in and registering from the same route. With that said what you are saying isn't really possible. Routing in Laravel is first come first served. Basically it checks the route until the URI matches one and then calls that method on the controller or executes the callback. Your routes have to be the other way in your routes file. This will be fixed by changing the url.
I am working on Kohana PHP framework.
I want to show a 'username' instead of controller name in my URL.
For example,
username = james then how to show
http://localhost:3000/james
instead of
http://localhost:3000/scrapbook/index => ... localhost:3000/scrapbook
(controller: scrapbook, action: index)
in the url.
My bootstrap file have the entry for such types of url. If I manually write ..//localhost:3000/james, it takes me to the requested page.
//Viewing a user's profile or account details - user/action
Route::set('profile', '(<username>(/<action>(/<id>)))',
array(
'username' => '([A-Za-z0-9\-]+)'))
->defaults(array(
'controller' => 'scrapbook',
'action' => 'index'));
What I want is if I manually signin and go to scrapbook, my url should show 'username' and not the name of the controller. I will appreciate if anyone can guide me in this.
Thanks
When you complete your sign in action, you'll want to redirect the user to the desired URL using reverse routing:
// ...in your controller
function action_signin()
{
// ...sign in logic
$this->request->redirect(
Route::get('profile')->uri(array(
'username' => $username
))
);
}
$username will be whatever the username of the user of the logged in user is that just signed in.