Redirecting in Laravel 4 - laravel-4

I am performing a functional test with Codeception powered by PHP unit.
I am coming from a form and would like to redirect back to the home page
$I->fillField('Username:', 'JohnDoe');
$I->fillField('Email:', 'john#example.com');
$I->fillField('Password:', 'demo');
$I->fillField('Password_confirmation', 'demo');
$I->seeCurrentUrlEquals('/');
$I->see('Register');
The code used for redirecting
Registration Controller
public function store()
{
return Redirect::to('/');
}
Route
Route::get('/', [
'as' => 'home',
'uses' => 'PagesController#home'
]);
In the browser the application works as expected but when I am running the functional test I get this error
--- Expected
+++ Actual
## ##
-'/'
+'/register'
It is as if the page is not redirecting from '/register' (registration page) to '/'(home page)
How can I rectify this

Have you tried Redirect::route('home') in the store() method?

Related

Laravel Jetstream Route Test With Inertia Returns Error Code 500

Out the test it works, I can visit the page and the controller wroks fine. I wrote the following test:
public function test_logged_user_is_not_redirected()
{
PartnerFactory::new()->create();
$request = $this->actingAs(UserFactory::new()->create())
->get('partners')
->assertRedirect('partners');
dd($request->inertiaProps());
}
I get error code 500. This is the controller:
public function index()
{
return Inertia::render('Partners/Index', [
'filters' => \Illuminate\Support\Facades\Request::all($this->getFilters()),
'contacts' => function() {
return $this->getAllContacts();
}
]);
}
This is the route in web.php
Route::get('partners', [PartnersController::class, 'index'])
->name('partners')
->middleware('auth');
Using refresh database, tried url with a '/' before, I still get 500.
edit: without exception handling i get: Trying to get property 'id' of non-object
Found the solution: The user in jetstream MUST have the personal team!

How to prevent an error on undefined routes in Laravel 5.5

I developed an API with Laravel 5.5. All is working fine.
But imagine that a user enter an "api" url directly in the browser (for example: api/books), then they will receive an error:
InvalidArgumentException
Route [login] not defined.
How to prevent this? I tried to add some routes in the routes/web.php file, but without success.
Or perhaps I should do nothing (there will be very few users who will do that)?
I found the answer here:
Laravel 5.5 change unauthenticated login redirect url
I only do that in the "app/Exceptions/Handler.php" file, I modified the function "render" like that :
public function render($request, Exception $exception)
{
// return parent::render($request, $exception);
// add dom
return redirect('/');
// or redirection with a json
/*
return response()->json(
[
'errors' => [
'status' => 401,
'message' => 'Unauthenticated',
]
], 401
);
*/
}
And it works fine. As the "Laravel" part will be used only as back end for APIs, it will be enough.

Laravel 5.2 custom middleware crashes localhost page

I have created a middleware to check Authenticated user's Database and restrict them accessing routes. But when i apply the middleware to the routes the localhost page crashes.
The localhost page isn’t working
localhost redirected you too many times.
public function handle($request, Closure $next)
{
$user = \Auth::user();
if (($user->plan == 'ip') && ($user->balance >= 1299)) {
return $next($request);
}
return redirect('no_balance');
}
This is my route
Route::group(['middleware'=>['auth','client','balance']],function(){
Route::get('/no_balance',[
'uses' => 'settingsController#noBalance',
'as' => 'no_balance',
]);
});
Balance is the middleware code i have included above and without balance middleware the application works fine. BUt when i include balance middleware it throws the error.
My controller code
public function noBalance(){
return view('no_balance')->with(compact('user_profiles','user_info'));
}
Your issue lies here:
Route::group(['middleware'=>['auth','client','balance']],function(){
Route::get('/no_balance',[
'uses' => 'settingsController#noBalance',
'as' => 'no_balance',
]);
});
Your balance middleware is checking if (($user->plan == 'ip') && ($user->balance >= 1299)) and if they fail this then you are redirecting them to 'no_balance' - which is also protected by this same middleware.
So you end up in a constant cycle of the middleware redirecting to 'no_balance' and then the middleware redirecting you again.
To fix this, just remove the 'no_balance' middleware from this route:
Route::group(['middleware'=>['auth','client']],function(){
Route::get('/no_balance',[
'uses' => 'settingsController#noBalance',
'as' => 'no_balance',
]);
});
I use this and it works. I hope it helps you. Instead of return redirect('no_balance'); I use this: abort(403,'Unauthorized action.');
which loads the 403 view in views/errors/403

Redirects an authenticated user back to the originally requested URL or the default URL in laravel

I am new at laravel and I want to achieve the following results, let's say a guest gets to the result page after searching for a term and then decides to login, how can I get the user to login and keep the same result page in laravel
I have the following code
in the filters.php I have the following:
Route::filter('guest', function()
{
if (Auth::check()) return Redirect::to('/');
});
then in the user controller I have the following
the show login function
public function login()
{
return View::make('users.login');
}
the handle login function
public function handleLogin()
{
$data = Input::only(['email', 'password']);
if(Auth::attempt(['email' => $data['email'], 'password' => $data['password']])){
return Redirect::to('/profile');
}
return Redirect::route('login')->withInput();
}
right now the default page after login goes to the profile page but I want the user to go back to wherever he was before login.
any help? thanks
I think that's what you looking for
return Redirect::back();
In Laravel 4, you can use Redirect::guest and Redirect::intended to achieve your target easily.
Redirect::guest put the current URL into the session before redirect to the target URL.
Redirect::intended check whether there is any URL saved in the session, redirect to that URL or a default location if it does not exist.
In action, your code can be:
if(Auth::attempt(['email' => $data['email'], 'password' => $data['password']])){
return Redirect::guest('/profile');
}
and after log in
if (Auth::check()) return Redirect::intended();

Laravel 4.2 form action not working

I'm new to Laravel and am building a simple web application. I'll show what code I'm using next and then I'll explain my problem.
Here's how my form starts in the view login.blade.php:
<?php
//build the form
echo Form::open(array('action' => 'AuthenticationController#authenticateUser'));
And here's what the route for the home page:
Route::get('/', function() {
return View::make('login', array('page_title' => 'Log in to MANGER'));
});
Finally, here's the authentication controller (for now it's a simple redirect to simulate login):
class AuthenticationController extends BaseController {
public function authenticateUser()
{
//retrive from database
return View::make('hr', array('page_title' => 'MANGER Login'));
}
}
My problem is, I'm getting an error on login.blade.php. saying: Route [AuthenticationController#authenticateUser] not defined. (View: /opt/lampp/htdocs/manger/app/views/login.blade.php)
How is the error about a route when I've defined a controller instead? And also, how can this be fixed? Please excuse any noob errors and thanks a lot in advance! :-)
You still need to define your route like this:
Route::post('authenticate', ['uses' => 'AuthenticationController#authenticateUser']);
Otherwise it won't know what method to use, or the url to create.

Resources