cakePHP Exclude PagesController Action from Auth - php

Hi first of all I'm using cakePHP 2.3.x
I'm having problem excluding my view pages from authentication. For example I have my static home page in Pages/home.ctp
In my AppController and PagesController I putted:
public function beforeFilter() {
$this->Auth->allow('home');
//$this->Auth->deny('add','edit','delete','index');
}
Yet it still requires me to log in.
I also putted in my PagesController
public function home(){
}
But still no luck.
Any help will be appriciated

please try with this in pages controller
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow("*");
}
OR try with $this->Auth->allow("display");

Instead of using PagesController I created a copy of it and name it with another name. I just deleted the display() function and put all other action there, and created the view and it runs good. I guest you can't just put a lot in PagesController.

Related

Any action is redirected to the root

I am using CakePHP for the first time and I have a trouble with requests.
I have for example this simple controller:
class TestController extends AppController
{
public function index() {
}
public function edit() {
}
public function view() {
}
}
Index is rendering right but when I call /test/edit or /test/view I am redirected (302) to the root of my webpage. Views for each action are created.
It's the same for any other controller in my app.
I cannot figure out why. Do you have any ideas, suggestion what should I check?
Thi was the problem $this->Auth->allow(['edit']); now it is working.

Laravel 5 Implicit Route Not Resoving

Editing this post entirely as I realize that I did a poor job of explaining things as they are, and to reflect a few changes already implemented.
The issue: I have an app that has a normal front end, which works perfectly when accessed via app\public. I've added a backend and wish to use a different master layout. I have named the backend Crud. I created Crud\UserController and that has the following:
public function __construct()
{ $this->middleware('auth'); }
public function getIndex() {
return view('crud'); }
In my routes.php file I have the following:
Route::controller('crud', 'Crud\UserController');
I've tried placing that route inside and outside of the middleware group. Neither workds. I do have a file, crud.blade.php, that exists inside resources\views.
The issue is a 404 from apache every time I try to access app/public/crud. Specifically, this error:
The requested URL /app/public/crud was not found on this server.
I'm at a loss as to why the server is unable to find the route to crud.blade.php
ETA: The apache access log just shows a normal 404 when I attempt to access this page. The apache error log shows no errors.
The view() is suppose to point to a view file, that is something like example.blade.php which should contain the content you want displayed when the localhost:app/crud is visited. Then you can do view('example') without the .blade.php.
From your code, change
class UserController extends Controller{
//Route::controller('crud', 'Crud\UserController');
public function __construct()
{
$this->middleware('auth');
}
public function getIndex() {
return view('/');
}
}
to
class UserController extends Controller{
//Route::controller('crud', 'Crud\UserController');
public function __construct()
{
$this->middleware('auth');
}
public function getIndex() {
return view('crud'); // crud being your view file
}
}
I think you're mixing concepts. Trying to get here will always throw an error:
localhost://app/crud
To enter your app, just try to access:
http://localhost
appending a route you registered in your routes.php file. In your example, It'd be:
http://localhost/crud
If you register
Route::get('/crud', 'Crud\UserController#index')
Then you need an Index method inside UserController, which should return a view (in your example)
class UserController extends Controller{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('crud');
}
}
P.S: about Implicit controllers, you have the docs here.

cakephp controller not working; view page not found

i installed fresh cakephp. its app controller working fine. but when i write my controller and its view but this result in page not found. My code is given below
class PostsController extends Controller {
public $helpers = array('Html', 'Form');
public function index() {
$this->set('posts', $this->Post->find('all'));
}
}
and create folder named Posts in view folder and over there create index.ctp file as well. but its result in page not found. Please help what may be mistake by me or is there any need to configuration. Thanks
Your controller should be named PostsController.php (ie plural)
Just spotted it, you need to extend AppController in CakePHP 2.x, not Controller: ie:
class PostsController extends AppController {
there might problem apache rewrite module. if you are using WAMP, just put on your rewrite module.

Direct access to URL redirects differently to using in anchor

I've got a very odd situation here and I'm hoping SO's collective knowledge can help.
I'm working on a legacy CakePHP 1.3 application, using the native Auth component to secure the majority of the app pages. However there are public pages which are accessible by anyone, and are set up in their controllers using $this->Auth->allow()
Now, we have a URL (using Cake's Router class) that if typed into a browser, goes straight to the expected page and everything works normally.
But, if you use the URL as the href of an anchor tag and click the link, you are bounced to the login page of our application as if you had visited a private URL.
I'm not sure that I can provide the exact URL at the moment though as this is client based public pages, not general public
We have debugged the routing all the way through and know that it redirects as soon as it hits the final controller action, not higher up in the inheritance chain.
Notes on environment:
The controller that handles this URL/action inherits from another controller which sets up our public page system
Its in a CakePHP plugin, not a direct controller
Its CakePHP 1.3
This only occurs in non-IE browsers. IE doesn't display this problem at all
Has anyone come across something similar and has a solution?
As requested in the comment, I've shared some of the code. Its heavily edited for brevity though so not complete
Public Controller
App::import('Controller', 'Events.Bookings');
class PublicBookingsController extends BookingsController
{
public function beforeFilter()
{
parent::beforeFilter();
$this->Auth->allow('*');
}
}
Booking Controller
class BookingsController extends EventsAppController
{
public new_booking()
{
die('Reached new booking');
}
}
Events App Controller
App::import('Controller', 'Occurrence');
class EventsAppController extends OccurrenceController
{
public function beforeFilter()
{
parent::beforeFilter();
// various plugin set up code
}
}
Occurrence Controller
class OccurrenceController extends AppController
{
public function beforeFilter()
{
parent::beforeFilter();
// generic plugin setup code snipped
}
}
Route
Router::connect('/path/to/url', array('plugin' => 'events', 'controller' => 'public_bookings', 'action' => 'new_booking');

Passing data to the view through the constructor method in Codeigniter

I am currently using the codeigniter tank_auth, at the start of every controller method I have to do the following:
$data['profile'] = $this->tank_auth->get_profile();
The main reason I do this is to display the current logged in username, and also get their privilege level.
I am going over the code trying to go by the DRY principle and have moved a lot of repeated code over to the _constructor method (Like checking if the user is logged in). I am just wondering if there is a way to move this code from the start of every method to the constructor.
My current constructor method looks like so:
public function __construct()
{
parent::__construct();
// If the user isn't logged in redirect to login page.
if (!$this->tank_auth->is_logged_in())
redirect('auth/login');
}
Thanks!
Add variable $data to the controller and use it for all your view data. For example:
public function __construct()
{
parent::__construct();
$this->data['profile'] = $this->tank_auth->get_profile();
}
When calling the view remember to call it like this:
$this->load->view('my_view', $this->data);
You can also extend CI_Controller with MY_Controller and put the login check in the constructor of MY_Controller. Just extend all controllers which need this check from MY_Controller instead of CI_Controller.

Categories