Laravel - Class Not Found - laravel

I am watching a series on tutsplus about building a real application with Laravel. The series was created last year. I assume it was for Laravel 3.
There were a lot of changes I assume and I found out most of them so far and replaced it from the old code. But I can not figure this one out.
In the following code, I get the error ReflectionException - Class questions does not exist even though the class I know is there. I did try composer update and auto-dumb but same output.
I assume there is something wrong with ether of these files:
Code:
QuestionsController.php
class QuestionsController extends BaseController {
public $restful = true;
public function get_index() {
return View::make('questions.index')
->with('title', 'Make it snappy Q&A - Home');
}
}
Route.php:
Route::get('/', array(
'as' => 'home',
'uses' => 'questions#index'
));
Why does it give an error?

that should work
Route::get('/', array(
'as' => 'home',
'uses' => 'QuestionsController#get_index'
));

Related

Laravel Dusk feature tests - getting 403 errors

I have a test file in the tests/Feature directory with the following contents:
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class UserTest extends TestCase
{
public function testA()
{
$response = $this->actingAs(\App\AdminUser::find(1))
->json('GET', '/users');
$response->assertStatus(200);
}
public function testB()
{
$response = $this->actingAs(\App\AdminUser::find(1))
->json('GET', '/users');
$response->assertStatus(200);
}
}
The problem is that when both tests are executed the assertion in the second one fails with the following error: Expected status code 200 but received 403. - Laravel throws an AccessDeniedHttpException for some reason.
If I comment out either test method the other one works.
Any ideas would be much appreciated.
--- Edit ---
Here's what's in the controller method:
public function index() {
return view('users.index');
}
And the definition in the routes file:
Route::group(['prefix' => 'users', 'as' => 'users.'], function () {
Route::get('/', ['as' => 'index', 'uses' => 'UserController#index']);
Route::get('datatables', ['as' => 'datatables', 'uses' => 'UserController#datatables']);
Route::get('show/{id}', ['as' => 'show', 'uses' => 'UserController#index']);
});
--- Edit 2 ---
Turns out this is an issue with the package I'm using. Further info: https://github.com/JosephSilber/bouncer/issues/306

Passing Object between Models

I have a a couple of Models like so (some code removed for simplicity)
class Poll extends Model
{
public function poll()
{
return $this->hasOne('App\PollQuestion', 'pollId');
}
}
class PollQuestion extends Model
{
public function poll()
{
return $this->belongsTo('App\Poll');
}
}
I then have all my routes set up, for the PollQuestion they currently look like this
Route::model('polls.questions', 'PollQuestion');
Route::get('/admin/polls/{id}/addquestions', [
'as' => 'polls.questions.create',
'uses' => 'PollQuestionController#addQuestions'
]);
Route::post('/admin/polls/{id}/storequestions', [
'as' => 'polls.questions.store',
'uses' => 'PollQuestionController#storeQuestion'
]);
In my PollQuestionController, to see the questions view I have
public function addQuestions(Request $request)
{
$poll = Poll::where('id', '=', $request->id)->first();
return view('poll.admin.questions', compact('poll'));
}
Inside this view if I dump the poll e.g.
{{ dd($poll) }}
I can see what I expect to see. For the question form, I am doing
{!! Form::model(new App\PollQuestion, [
'route' => ['polls.questions.store', $poll]
]) !!}
So I presume that should pass my store function the Poll Object I previously dumped. However, in the store function, I do
public function storeQuestion(Request $request, Poll $poll) {
dd($poll);
}
And it shows Null. Why would this happen seeing that I am passing it the Poll Object I had previously dumped?
Thanks
You need use Route Model Binding correctly. According to Laravel Documentation
Route::model('poll-question', App\PollQuestion::class);
Route::get('/admin/polls/{poll-question}/addquestions', [
'as' => 'polls.questions.create',
'uses' => 'PollQuestionController#addQuestions'
]);
Route::post('/admin/polls/{poll-question}/storequestions', [
'as' => 'polls.questions.store',
'uses' => 'PollQuestionController#storeQuestion'
]);
Route::model define a variable in the route like a Model. This method searches the primary key passed in the route and retrieve the Model.

Getting NotFoundHttpException for my edit route

This is my route
Route::get(
'account-executive/{$id}/edit',
array(
'as' => 'vendor-edit',
'uses' => 'AdminController#updateVendor'
)
);
This is my method
public function updateVendor($id)
{
$vendor = Vendor::findOrFail($id);
return View::make('admin.edit-account-executive');
}
I keep getting NotFoundHttpException. Any ideas as to why?
Your route definition isn't correct.
Route::get('account-executive/{$id}/edit', array('as' => 'vendor-edit','uses' => 'AdminController#updateVendor'));
You don't need a $ sign in definition of route parameter. So you should just write account-executive/{id}/edit.
Try it.

Controller in subfolder with namespace in laravel-4.1

I have the following code:
Route::group(array('namespace' => 'admin'), function() {
Route::group(array('prefix' => 'admin'), function() {
Route::get('group', array('as' => 'adminGroup', 'uses' => 'GroupController#index'));
Route::get('group/index', array('as' => 'adminGroupIndex',
'uses' => 'GroupController#index'));
});
});
and controller
namespace admin;
class GroupController extends \BaseController {
protected $layout = 'dashboard';
public function index()
{
$this->layout->content = \View::make('admin/group/index');
}
}
If I point the URL to:
http://localhost/laravel/public/admin/group/index
works perfectly, but when I point to:
http://localhost/laravel/public/admin/group
does not work. It just redirects to:
http://localhost/laravel/public/user/login
But when I do not use subfolder everything works perfectly!
EDIT: SOLVED
I had started installing laravel administrator and then stopped, because there was not installed an authentication system. So I installed Sentry2 and was configuring management groups. After analyzing a little more settings Laravel Administrator, I realized that it was using the URI 'admin' and also redirected to 'user / login' if I was not authenticated.
Now everything is working perfectly!
You probably have another route filtered with "auth" that is catching that /admin/group URL and sending it to login.
I just reproduced your code here and it works fine for me. For the sake of simplicity I just replaced my routes.php file with this code:
<?php
namespace admin;
class GroupController extends \Controller {
protected $layout = 'dashboard';
public function index()
{
return 'index!';
}
}
\Route::group(array('namespace' => 'admin'), function() {
\Route::group(array('prefix' => 'admin'), function() {
\Route::get('group', array('as' => 'adminGroup', 'uses' => 'GroupController#index'));
\Route::get('group/index', array('as' => 'adminGroupIndex',
'uses' => 'GroupController#index'));
});
});
And both
http://development.consultoriodigital.net/admin/group
http://development.consultoriodigital.net/admin/group/index
Worked fine showing a page with
index!

troubleshooting Cakephp auth component not allowing allowed actions

Sept 2 update:
This has become a very difficult puzzle to solve. Setting up a basic auth, which is all that I want, should involve very few steps. I have done many tests, adding and removing code, reviewing the cake manual, reading tutorials and going step by step through the cakePHP 1.3 application development cookbook by Mariano Iglesias - good book. http://goo.gl/93BGw
But the problem I'm still facing is that the app controller is the only place the 'allowed' actions work. In individual controllers the parent:beforeFilter doesn't get recognized and I'm redirected back to the users login page.
Any help with this is really appreciated. What I'm wondering is how I might debug this type of problem. Are there any other configuration settings I should look at, like 'prefix routing'?
=======================
Sept 1 update:
After a lot of testing what appears to be the issue is that the 'before:filter' in individual controllers isn't being recognized. Example in the post controller:
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow = array('edit');
}
Has anyone had this happen before? I've referred to the cakePHP manual as well as many online articles and tutorials and it doesn't make any sense to me. I've even tried to build a simple application with just the users and post controller and still, the before:filter settings in each controller aren't being recognized.
=======================
Original question.
I am using the Cakephp auth component to manage an admin section. This is using version 1.3.11
The problem I'm having is that even with allowed actions in each controller, I'm being redirected to the user login page.
Here is what's in the app controller:
class AppController extends Controller {
var $components = array(
'Auth' => array(
'authorize' => 'controller'
),
'Session',
'RequestHandler'
);
public function isAuthorized() {
return true;
}
function beforeFilter(){
$this->Auth->authorize = 'controller';
$this->Auth->fields = array('username' => 'username', 'password' => 'password');
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
$this->Auth->authError = 'Please login to view that page ';
$this->Auth->loginError =' The user name or password you entered did not work, please try again ' ;
$this->Auth->allow('display');
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
$this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'logout');
$this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'display', 'home');
}
This is what's in the users controller:
class UsersController extends AppController {
var $name = 'Users';
function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow = array('add');
}
This is what's in the posts controller:
class PostsController extends AppController {
var $name = 'Posts';
var $components = array('Session','RequestHandler', 'Email');
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow = array('edit');
}
What I do find is that after I've logged in I'm able to access the home page, as expected. Then when I go to the logout the session isn't entirely destroyed so I can go back to the 'admin' section.
I did try using $this-session('destroy'); in the logout action, but when I did the allowed actions didn't work again.
Does this make sense? Shouldn't allowed actions be independent of a current session?
Thanks, Paul
Make sure you are not using requestAction in any of your elements or views, make sure that the actions called by requestAction are allowed too.... this should fix it.
For the one when you logout and I can still access admin section: the logout() should have $this->redirect($this->Auth->logout()); It should clear the Auth session data.
Here's what I suggest for the beforeFilter() in appcontroller:
function beforeFilter(){
$this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'dashboard');
}
and for the pages controller: $this->Auth->allow('display', 'view');

Resources