$this->layout = 'bootstrap'
The code above is written on my index function which was accessible even without login.
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('index');
}
public function index() {
$this->layout = 'bootstrap';
}
The code above does not work, I tested. (many times)
public function beforeFilter() {
parent::beforeFilter();
$this->layout = 'bootstrap';
$this->Auth->allow('index');
}
public function index() {
}
The above code also does not work. The bootstrap layout is rendered fine when logging in, but when I log out and access it again, the view is rendered as basic HTML.
Is there anything I'm doing wrong here?
Solution:
I got it, it was the authentication for logging in that was keeping the path for bootstrap from being rendered.
Since I was accessing the view without logging in, I just have to move that specific path outside the authentication clause.
Related
I am working with Codeigniter PHP.
I want if admin not login then he can't see admin panel pages.
For this i created helper.
The Code is working if I put condition is every function but if I use this code in "constructor" then redirect to home page but also showing "login" page after home page.
Where I am wrong ?
Here is my code (using in one of my function which is working )
public function admin_offers(){
if (!is_logged_in()) {
$this->index();
//die();
}
else {
$data['offers']=$this->db->get('offers')->result();
$this->load->view('admin/special-offers',$data);
}
}
And here is my code in constructor, which is not working for me.
public function __construct() {
parent::__construct();
$this->load->model("stores_model");
$this->load->library("pagination");
$this->load->library('encryption');
is_logged_in();
if (!is_logged_in()) {
$this->index();
}
}
First, from your code, you are calling the function twice which is unnecessary.
is_logged_in(); //this calls the function
if (!is_logged_in() /* this too */) {
$this->index();
}
For your case, try this:
function __construct(){
parent::__construct();
$this->load->model("stores_model");
$this->load->library("pagination");
$this->load->library('encryption');
if (!$this->session->user_data('session_name')) {
redirect('controller_name/function'); // the public page
}
}
Basically, what the code does, it checks if the session data(set during login) exists, if it does not exist, the user is redirected to the public page(possibly the login page).
If you place the code under the __construct function, no need to include it in individual functions as this code is executed when the class is instantiated, hence executed before the functions are loaded.
NB: Make sure you have already loaded session library in autoload
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.
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.
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.
I am using CakePHP 2.0. I have used the following code in my AppController.php
public function beforeFilter() {
parent::beforeFilter();
$this->setOrderConfigValues();
}
public function setOrderConfigValues(){
$this->Session->write('Order.session', $this->Session->read('Config.userAgent'));
}
but its not working. When I apply die() inside the function, it displays the value, but, when using normally, function is not called.
You probably have beforeFilter() override in some other controller, but you forgot to add parent::beforeFilter();