which is the best way for adding maintenance mode in laravel? - laravel

i want to add maintenance mode to my cms which i built with laravel,
i want to add front part of it, but i don't know which is the best place for checking maintenance mode, in my basic layout file or in a middleware or somewhere else.
consider it that this check should be run on each request on my frontend routes, therefor it needed to be super optimized.
please remind that i want to enable this capability on admin panel of cms, so the admin user doesn't has permission on built in laravel maintenance mode.
and i want it to be okay with a shared host environment. (i have no access to shell commands)
any idea?
i tried to put it on my blade file and in a middleware, but i think it's not optimized.

You could add a Middleware to the Front end routes only. When someone enables maintenance mode in the admin area the middleware could check if maintenance mode is enabled and redirect or show a view
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class MaintenanceMode
{
public function handle($request, Closure $next)
{
if (/** Logic Here **/) {
// Show a diff page
}
return $next($request);
}
}

Related

Basic way to do user authentication for codeigniter 3

My current solution is to:
Create a authentication_model.
and write my authentication in the __constructor of each controller that needs a user logged in. Very simple and easy to understand.
My other option is to make a My_controller and do my authentication there and extend it with all controllers that need a user logged in.
Problem is, the answers ive gotten from this varies, some only use My_controller but some have base_controller and auth_controller.
And i'm confused if a single My_controller for authentication is enough or not.
Let's assume you are using Ion_Auth (I can recommend it). Now, if your site is 99% behind an auth system a MY_Controller with a __construct that looks like this should be sufficient for all of the controllers requiring auth:
class MY_Controller extends CI_Controller {
public function __construct() {
if (!$this->ion_auth->logged_in()) {
redirect('login');
}
}
}
and then in the login controller (e.g. user isn't logged in yet) you simply extend CI_Controller so you don't end up in a redirect loop.
The issue with this approach becomes apparent if you are like me and have separate frontend and backend running on the same CI installation. In such a case you have you public controllers in your main CI controllers directory and a sub-directory in controllers called auth. Again, in my case, the frontend and backend share similar resources but I don't want to bog down the frontend by autoloading Ion_Auth and the users_model and all the models that make my backend work as its not required functionality.
So I only autoload things I use in both such as sessions and database and then have a Shared_Controller that extends CI_Controller that sets up some public variables that I use in both. Then I have a Auth_Controller that extends the Shared_Controller and uses a similar approach as the code above; in that __construct I load what I would have autoloaded had I not had a "separate" frontend (such as Ion_Auth .etc.) and just make the login controller a special case that does not get redirected.
I then also have a Frontend_Controller also extending Shared_Controller that loads some things I use throughout the frontend controllers.
So as you can see it largely depends on what you are trying to accomplish. There is no right or wrong way just left and right.

Laravel 5.3 determine redirection based on factors

I realize that I can change where my user is directed after login by changing
protected $redirectTo = '/home';
in the LoginController.php
However, I want to check if there are any items in a shopping cart and if so, direct the user to the checkout page. I may also check to see if they have purchased a service that is active and direct them to the dashboard, then anyone else to the homepage.
Anyone know how to apply some logic to the redirection?
You can override sendLoginResponse() method in app\Http\Controllers\Auth\LoginController.php to perform the check and redirect to whatever route you want.
Original sendLoginResponse() method is in vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesUsers.php. Copy-paste it to LoginController.php and then work with it. Do not change anything in an original trait.

Laravel - sharing controllers while changing the redirect route and views?

I have a banned user controller in the moderation panel where a moderator can ban a specific user. In the admin panel, I have to use the same views and same controller methods. I don't want to duplicate the controller for admin panel and change the redirect route and stuff.
For instance, in my BanController, I have the following:
public function index() {
return view('mod.ban.index')
}
public function ban(User $user) {
// Ban user
// Redirect back to the new ban page
redirect(route('mod.ban.show', $user->ban->id));
}
public function unban(User $user) {
// Unban user
// Redirect back to the new ban page
redirect(route('mod.ban.show, $user->ban->id));
}
As you can see, the redirect route and views are for moderation only. The views extend to the mod layout in moderation panel. I cannot find a way to share the views between the admin and mod panel unless I store the contents in a partial view. I can overcome this problem easily but the redirection is the main problem here. If I use one controller for both, when it comes to redirection, the admin may be redirected to the mod panel while he is managing a ban from admin panel and vice versa.
How can I best solve this problem? Thank you!
How can I use the same controller logic and use it in both moderation panel and admin panel? Thanks a ton!

How do I change the index page in a CodeIgniter anchor?

So, I have two different applications in my CodeIgniter installation. One is admin, the other is frontend. I basically just copied the index file, renamed it "admin.php", and changed the application directory to "application/admin". I then changed the application directory in index.php to "application/frontend".
What I would like to do is create a link on the frontend application that takes you to the admin application. The variable config['index_page'] in the frontend application is set to "index.php" and in the admin application it's set to "admin.php".
Is there a way to set the url helper to use "admin.php" instead of "index.php"?
You don't need to do that way.
you should make or use an authentication library and you set different roles for different
users.
you just after login can put the redirection to your admin controller.
and for other users and viewers you can redirect them to any other controllers.
for viewers you can just use something like this:
Code:
if(!$this->m_auth->is_logged_in())
{
$this->viewers();
}
else
{
$this->users();
}
In your users function you just check different roles and redirect according.
I think you are missing some codeigniter concept, and you are trying to do it the normal way, i suggest you to read this article , you will how you can use MY_Controller as same concept of front controller and how you will be able to give every use specific roles
another way is to use a ready made authentication library as #medhi said
I would recommend Tank Authentication or Ion Auth
I

Separate Admin and Front in codeigniter

What is the best way to separate admin and front-end for a website in codeigniter where as I was to use all libraries, models, helpers etc. in common, but only controllers and Views will be separate.
I want a more proper way, up for performance, simplicity, and sharing models and libraries etc.
I highly suggest reading the methods outlined in this article by CI dev Phil Sturgeon:
http://philsturgeon.co.uk/blog/2009/07/Create-an-Admin-panel-with-CodeIgniter
My advice: Use modules for organizing your project.
https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/wiki/Home
Create a base controller for the front and/or backend. Something like this:
// core/MY_Controller.php
/**
* Base Controller
*
*/
class MY_Controller extends CI_Controller {
// or MX_Controller if you use HMVC, linked above
function __construct()
{
parent::__construct();
// Load shared resources here or in autoload.php
}
}
/**
* Back end Controller
*
*/
class Admin_Controller extends MY_Controller {
function __construct()
{
parent::__construct();
// Check login, load back end dependencies
}
}
/**
* Default Front-end Controller
*
*/
class Public_Controller extends MY_Controller {
function __construct()
{
parent::__construct();
// Load any front-end only dependencies
}
}
Back end controllers will extend Admin_Controller, and front end controllers will extend Public_Controller. The front end base controller is not really necessary, but there as an example, and can be useful. You can extend MY_Controller instead if you want.
Use URI routing where needed, and create separate controllers for your front end and back end. All helpers, classes, models etc. can be shared if both the front and back end controllers live in the same application.
I use a very simple approach: file folders. Check out the CI User Guide section, Organizing Your Controllers into Sub-folders.
I have my public-facing website built as any other would be built with CodeIgniter. Then I have two additional folders, controllers/admin and views/admin.
The admin controllers are accessed via http://[hostname]/admin/controller, and behave just as any other controller except they have specific authentication checks. Likewise, the views are simply called with the folder name included: $this->load->view('admin/theview');.
I haven't found a reason to do anything more complicated than that.
You all can find complete solution over here, https://github.com/bhuban/modular
Module separation for admin and front-end using HMVC and template separation using template libraries
I am using two third party libraries, you can find it in zip file.
HMVC for modular developed by wiredesignz
Template engine for templating by Phil Sturgeon
Just unzip it into your webserver root directory and run
localhost/modular for front-end
and
localhost/modular/admin for back-end
application/back-modules, it is for the back-end modules
application/front-modules, it is for the front-end modules
similarly
templates/admin for the back-end templates
templates/front for the front-end templates
themes/admin for the back-end themes
themes/front for the front-end themes
Nothing hacked in original code just configured using config.php and index.php

Resources