Laravel Route group issue - php

I have been using laravel for a while now, but I stumbled across an error which I have never encountered before. It is probably me overlooking it, but with the route file given below, the route group with the prefix account gives a blank page. When going to /account/anunregisteredroute it does give a httpnotfoundexception
My routes.php file:
http://pastebin.com/EnnGSm10

By adding a / before the parameter you can solve this issue:
Route::get('/{username}', ['as' => 'account-profile', 'uses' => 'AccountController#getProfile']);
This piece of code worked for me:
Route::group(['prefix' => 'account'], function () {
Route::get('/{username}', ['as' => 'account-profile', 'uses' => function($username){
echo $username;
}]);
Route::get('profile', ['as' => 'account-edit-profile', 'uses' => 'AccountController#getUpdate', 'before' => 'auth']);
Route::post('profile', ['as' => 'account-edit-profile', 'uses' => 'AccountController#postUpdate', 'before' => 'auth|csrf']);
Route::group(['before' => 'guest'], function () {
Route::get('create', ['as' => 'account-create', 'uses' => 'AccountController#getCreate']);
Route::get('signin', ['as' => 'account-signin', 'uses' => 'AccountController#getSignin']);
Route::group(['before' => 'csrf'], function() {
Route::post('create', ['as' => 'account-create', 'uses' => 'AccountController#postCreate']);
Route::post('signin', ['as' => 'account-signin', 'uses' => 'AccountController#postSignin']);
});
});
});
I got the expected output.

Related

Laravel saying route not defined but it is?

InvalidArgumentException Route [welcome.begin] not defined.
But it does? Happens when visiting /welcome/begin and I have double checked the file does exist there.
Routes:
<?php
Route::group(['domain' => 'localhost', 'namespace' => 'Frontend'], function () {
Route::group(['middleware' => 'guest', 'namespace' => 'Guest'], function() {
Route::group(['prefix' => 'welcome'], function() {
Route::any('/begin', ['as' => 'frontend.welcome_begin', 'uses' => 'WelcomeController#begin']);
Route::any('/language', ['as' => 'frontend.welcome_language', 'uses' => 'WelcomeController#language']);
Route::any('/final', ['as' => 'frontend.welcome_final', 'uses' => 'WelcomeController#final']);
});
Route::any('/', ['as' => 'login', 'uses' => 'LandingController#redirect']);
Route::get('/login', ['as' => 'frontend.login', 'uses' => 'LoginController#getView']);
Route::post('/login', ['as' => 'frontend.login', 'uses' => 'LoginController#onPost']);
});
Route::group(['middleware' => 'auth', 'namespace' => 'User'], function() {
Route::get('/home', ['as' => 'frontend.home', 'uses' => 'HomeController#getView']);
});
});
WelcomeController:
<?php
namespace App\Http\Controllers\Frontend\Guest;
use App\Http\Controllers\Controller;
use Redirect;
use Illuminate\Http\Request;
class WelcomeController extends Controller
{
public function begin()
{
return route('frontend.welcome.begin');
}
}
View Config:
<?php
return [
'paths' => [
resource_path('views'),
],
'compiled' => realpath(storage_path('framework/views')),
];
There error happens on the "route("frontend.welcome.begin")" line
The route does not exist. Your route is named as frontend.welcome_begin but you are calling frontend.welcome.begin
Your code will need to be: return route('frontend.welcome_begin');
I will suggest, having a brief look at the code, to change your welcome routes to be frontend.welcome.{name} rather than using an underscore, as it will follow the rest of the route names you've defined
First you have check if route name is correct
Open terminal
Go to project cd PATH_TO_PROJECT
write php artisan route:list
Check what is correct route name.

Middleware auth not working with web Laravel 5.2

Hi I am having some problem with authentication in laravel. I have to use two middleware 1. is web and 2. auth . I am using web middleware so that I can use session to show flash messages. and want to use auth middleware to do authentication of users/admin. but I am facing some problems.
below is my function to check authorization and to redirect to their respective routes
public function postLoginForm(){
$email=Input::get('email');
$password=Input::get('password');
$data=[
'email'=>$email,
'password'=>$password
];
$rules=[
'email'=>'required',
'password'=>'required'
];
$validator=Validator::make($data,$rules);
if($validator->fails()){
Session::flash('fail', 'Oops Something went wrong!!');
return redirect()->back()->withErrors($validator);
}
else{
if(Auth::attempt($data)){
$checkStatus=User::select('*')->where('email',$email)->first();
Session::put('email',$checkStatus->email);
Session::put('user_type',$checkStatus->user_type);
if($checkStatus['user_type']=='4'){
if($checkStatus['status']=='0'){
Session::flash('wait', 'Registration is not approved!!');
return "student";
return redirect()->back();
}
else{
return "student else";
return Redirect::route('get.student.dashBoard');
}
}
else if($checkStatus['user_type']=='1'){
return Redirect::route('get.admin.dashBoard');
}
else if($checkStatus['user_type']=='2'){
return 'admin sir view';
return Redirect::route('get.admin.dashBoard');
}
else if($checkStatus['user_type']=='3'){
return 'admin other view';
return Redirect::route('get.admin.dashBoard');
}
else{
Session::flash('fail', 'Oops Something went wrong!!');
return redirect()->back();
}
}
else{
Session::flash('fail', 'Login details not matched!!');
return redirect()->back();
}
}
return 'nothing works';
}
below is my routes for admin
Route::group(['middleware' => ['web']], function () {
Route::get('/login',
['as' => 'get.login.page',
'uses' => 'LoginController#getLoginPage']);
Route::post('/login-done',
['as' => 'post.login.page',
'uses' => 'LoginController#postLoginForm']);
Route::get('/register',
['as' => 'get.register.page',
'uses' => 'LoginController#getRegisterPage']);
Route::post('/register',
['as' => 'post.register.form',
'uses' => 'LoginController#postRegisterForm']);
Route::get('/forgot-password',
['as' => 'get.forgotPassword.form',
'uses' => 'LoginController#getForgotPasswordForm']);
Route::group(['middleware' => ['auth']], function () {
Route::get('/admin-dashboard',
['as' => 'get.admin.dashBoard',
'uses' => 'admin\PageController#getAdminDashboard']);
Route::get('/all-achievements',
['as' => 'get.achievements',
'uses' => 'admin\AchievementsController#getAchievementsList']);
Route::get('/new-achievement',
['as' => 'get.add.achievement',
'uses' => 'admin\AchievementsController#getAddAchievement']);
Route::post('/add-achievement',
['as' => 'post.achievementsForm',
'uses' => 'admin\AchievementsController#postAchievements']);
Route::get('remove-achievement/{achie_slug}',
['as' => 'post.delete.achievements',
'uses' => 'admin\AchievementsController#postDeleteAchievement']);
Route::get('edit-achievement/{achie_slug}',
['as' => 'get.edit.achievements',
'uses' => 'admin\AchievementsController#getEditAchievement']);
Route::post('update-achievement/{ach_id}',
['as' => 'post.edited.achievement',
'uses' => 'admin\AchievementsController#postEditedAchievement']);
Route::get('/all-news',
['as' => 'get.news.list',
'uses' => 'admin\NewsController#getNewsList']);
Route::get('/add-news',
['as' => 'get.add.news',
'uses' => 'admin\NewsController#getAddNews']);
Route::post('/add-news',
['as' => 'post.add.news',
'uses' => 'admin\NewsController#postAddNews']);
Route::get('/delete-news/{news_slug}',
['as' => 'get.delete.news',
'uses' => 'admin\NewsController#postDeleteNews']);
Route::get('/edit-news/{news_slug}',
['as' => 'get.edit.news',
'uses' => 'admin\NewsController#getEditNews']);
Route::post('/edit-news/{news_slug}',
['as' => 'post.edited.news',
'uses' => 'admin\NewsController#postEditedNews']);
Route::get('/all-admins',
['as' => 'get.admin.list',
'uses' => 'admin\AdminController#getAllAdminList']);
Route::get('/add-admin',
['as' => 'add.new.admin',
'uses' => 'admin\AdminController#getAddNewAdmin']);
Route::post('/add-new-admin',
['as' => 'post.add.new.admin',
'uses' => 'admin\AdminController#postAddNewAdmin']);
Route::get('/all-schedule',
['as' => 'get.timeTable.list',
'uses' => 'admin\TimeTableController#getTimeTableList']);
Route::get('/add-schedule/{id}',
['as' => 'add.timeTable',
'uses' => 'admin\TimeTableController#getAddNewBatch']);
Route::post('/add-new-batch',
['as' => 'add.newBatch',
'uses' => 'admin\TimeTableController#postAddNewBatch']);
Route::post('/save-year-batch',
['as' => 'save.year.batch',
'uses' => 'admin\TimeTableController#postSaveYearBatch']);
Route::get('/schedule-table/{year}',
['as' => 'view.schedule.table',
'uses' => 'admin\TimeTableController#getScheduleTable']);
Route::get('/delete-schedule/{slug}',
['as' => 'delete.schedule.one',
'uses' => 'admin\TimeTableController#postDeleteOneSchedule']);
Route::get('/edit-schedule/{slug}',
['as' => 'edit.schedule.one',
'uses' => 'admin\TimeTableController#getEditScheduleForm']);
Route::post('/save-edited-schedule/{id}',
['as' => 'save.edited.schedule',
'uses' => 'admin\TimeTableController#postEditScheduleForm']);
Route::get('/all-results',
['as' => 'get.all.results',
'uses' => 'admin\ResultsController#getAllResults']);
Route::get('/add-result',
['as' => 'get.add.results',
'uses' => 'admin\ResultsController#getAddResult']);
Route::post('/add-new-result',
['as' => 'post.add.result',
'uses' => 'admin\ResultsController#postAddResult']);
Route::get('/delete-result/{id}',
['as' => 'get.delete.student.result',
'uses' => 'admin\ResultsController#getDeleteResult']);
Route::get('/edit-result/{id}',
['as' => 'get.edit.student.result',
'uses' => 'admin\ResultsController#getEditResult']);
Route::post('/save-edited-result/{id}',
['as' => 'post.edited.result',
'uses' => 'admin\ResultsController#postEditedResult']);
Route::get('/contact-messages',
['as' => 'get.contact.message',
'uses' => 'admin\ContactMessageController#getAllContactMessages']);
Route::get('/contact-messages/{id}',
['as' => 'get.delete.contact.message',
'uses' => 'admin\ContactMessageController#getDeleteContactMessages']);
});
});
every time i try to login it redirects me to the same login page. please guide me whats wrong with this.
You should remove web middleware from middleware group to make it work. It applies to all routes inside web.php (5.3) and routes.php (5.2.27 and higher) automatically and if you'll add it manually, it will break session related functionality.

Laravel - root directory routing behaves strangely in a nested route group

I have the following route definition in Laravel 5. When I group the routes in the following way, it seems the route admin.proposals.home will not work if I provide "/" as the get path, and will work if something trailing (like home) is provided:
/**
* Routes for system administrators.
*/
Route::group(['prefix' => 'admin', 'middleware' => 'admin'], function()
{
Route::group(['prefix' => 'people'], function()
{
Route::get('/', ['as' => 'admin.people.home', 'uses' => 'PersonController#index']);
Route::get('/profile/{userName}', ['as' => 'admin.person.profile', 'uses' => 'PersonController#view']);
Route::get('/organization/{id}', ['as' => 'admin.people.organization', 'uses' => 'PersonController#indexByOrganization']);
});
Route::group(['prefix' => 'projects'], function()
{
Route::get('/', ['as' => 'admin.projects.home', 'uses' => 'ProjectController#index']);
Route::get('/{projectId}', ['as' => 'admin.project.view', 'uses' => 'ProjectController#view']);
Route::group(['prefix' => 'proposals'], function()
{
//problematic line below
Route::get('/home', ['as' => 'admin.proposals.home', 'uses' => 'ProposalController#index']);
Route::get('/{proposalId}', ['as' => 'admin.proposal.view', 'uses' => 'ProposalController#view']);
});
});
});
Specifically, if I change the line:
Route::get('/home', ['as' => 'admin.proposals.home',
'uses' => 'ProposalController#index'
]);
To:
Route::get('/', ['as' => 'admin.proposals.home',
'uses' => 'ProposalController#index'
]);
I got an error saying:
Trying to get property of non-object (View: ... \views\admin\projects\view.blade.php)
But, the admin.proposals.home route points to the controllers index() method and has nothing to do with the view.blad.php.
Changing the path back to get('/home') works perfectly.
What is it that I'm missing?
It's a Route position order problem.
Since you have the route for the URI admin/projects/ as admin.project.home, and after that admin/projects/{projectId} it takes precedence over the route admin/projects/proposals/
Laravel takes proposals as the projectId.
Route::group(['prefix' => 'projects'], function()
{
Route::group(['prefix' => 'proposals'], function()
{
//problematic line below
Route::get('/', ['as' => 'admin.proposals.home', 'uses' => 'ProposalController#index']);
Route::get('/{proposalId}', ['as' => 'admin.proposal.view', 'uses' => 'ProposalController#view']);
});
Route::get('/', ['as' => 'admin.projects.home', 'uses' => 'ProjectController#index']);
Route::get('/{projectId}', ['as' => 'admin.project.view', 'uses' => 'ProjectController#view']);
});
Try this order and let me know what you get. But purely it's a route order problem.

Laravel 4: route in group throwing notfoundhttpexception

I've noticed a bit of a peculiarity in Laravel 4 when using Routes. I have a Route group that looks like this:
// Employers routes
Route::group(array('prefix' => 'employers'), function(
Route::get('/', array('as' => 'employers.index', 'uses' => 'EmployersController#index'));
Route::get('create', array('as' => 'employers.create', 'uses' => 'EmployersController#create'));
Route::post('/', array('as' => 'employers.store', 'uses' => 'EmployersController#store', 'before' => 'csrf'));
Route::get('search', array('as' => 'employers.search', 'uses' => 'EmployersController#search'));
Route::get('{id}', array('as' => 'employers.show', 'uses' => 'EmployersController#show'));
Route::get('{id}/edit', array('as' => 'employers.edit', 'uses' => 'EmployersController#edit'));
Route::patch('{id}/update', array('as' => 'employers.update', 'uses' => 'EmployersController#update', 'before' => 'csrf'));
Route::delete('{id}/destroy', array('as' => 'employers.destroy', 'uses' => 'EmployersController#destroy', 'before' => 'csrf'));
));
I've noticed, however, that when I try and add in a new route I have to add it before the first route to use the {id} wildcard as the first parameter in it's url, otherwise I get a notfoundhttpexception. Is this normal? So for example, this works (adding in the employers.search route:
// Employers routes
Route::group(array('prefix' => 'employers'), function(
Route::get('/', array('as' => 'employers.index', 'uses' => 'EmployersController#index'));
Route::get('create', array('as' => 'employers.create', 'uses' => 'EmployersController#create'));
Route::post('/', array('as' => 'employers.store', 'uses' => 'EmployersController#store', 'before' => 'csrf'));
Route::get('{id}', array('as' => 'employers.show', 'uses' => 'EmployersController#show'));
Route::get('search', array('as' => 'employers.search', 'uses' => 'EmployersController#search'));
}
Results in the route employers.search not being found?
This is expected behavior. Routes are evaluated in a top-down fashion.
{id} is a "catch all" route.
So the route system sees /search - and thinks search is an {id} - so it loads that route. But then it cannot find an id of search - and so it fails.
So keep your "catch all" route at the bottom of the list - and it will work correctly.

Laravel 4: named routes in named groups

Is there a way to make something like this?
Route::group(array('as' => 'admin', 'prefix' => 'admin', 'before' => 'admin'), function()
{
Route::get('/', array('as' => 'home', 'uses' => 'AdminController#index'));
Route::get('users', array('as' => 'users', 'uses' => 'AdminController#users'));
});
The goal is to do not include "admin" in all names and make links for above example like this:
URL::route('admin.home');
URL::route('admin.users');
Above example doesn't work:
Illegal offset type in unset
laravel/bootstrap/compiled.php:5053
Named group with nonamed routes inside works.
Named routes in nonamed group work too.
But not together.
Route::group(['prefix' => 'admin', 'before' => 'adminAuth'], function(){
// If you do not want to repeat 'admin' in all route names,
// define the value here
$r = 'admin';
Route::get('users', ['as' => "{$r}.users", 'uses' => 'AdminController#users']);
Route::get('/', ['as' => "{$r}.root", 'uses' => 'AdminController#index']);
});
In yout views/redirect you can use URL::action('ControllerName#method) and Laravel will know where redirect/point to...

Categories