I have to create an application with 5 types of roles.
So I started nesting my controllers
Controllers
administrator/establishments.php
supervisor/establishments.php
Views
administrator/establishments/index.php
supervisors/establishments/index.php
But I noticed that they have almost THE SAME CODE and the count of files will be huge!
I have been thinking in some ways to solve this
Controllers
establishments.php
and then ask:
if (Request::is('admin/*'))
{
Establishments::paginate(20);
}
if (Request::is('supervisor/*'))
{
Establishments::where_country(1)->paginate(20);
}
same for views.
Save role on session info, and create a menu to switch the var from one user to another
$role = Session::get('role');
if ($role == 'admin'))
{
Establishments::paginate(20);
}
if ($role == 'supervisor'))
{
Establishments::where_country(1)->paginate(20);
}
Any other ideas or suggestions?
What you could do is build a 'BaseCOntroller' wich has all the required logic in it.
On top of that you create the 5 other controllers - extend them on the BaseController.
Via the __construct() method you will pass the type so the BaseController knows what it has to do.
Related
In my application, users can belong to different accounts and have different roles on those accounts. To determine which account is "current" I am setting a session variable in the LoginController in the authenticated() method.
$request->session()->put('account_id', $user->accounts()->first()->id);
Then, throughout the application I am doing a simple Eloquent query to find an account by ID.
While this "works", I am basically repeating the same exact query in every single Controller, Middleware, etc. The maintainability is suffering and there are duplicate queries showing in Debugbar.
For example, in every controller I am doing:
protected $account;
public function __construct()
{
$this->middleware(function($req, $next){
$this->account = Account::find($req->session()->get('account_id'));
return $next($req);
});
}
In custom middleware and throughout the entire application, I am essentially doing the same thing - finding Account by ID stored in session.
I understand you can share variable with all views, but I need a way to share with the whole application.
I suppose much in the same way you can get the auth user with auth()->user.
What would be the way to do this in Laravel?
I would create a class to handle this logic. Making it a singleton, to ensure it is the same class you are accessing. So in a provider singleton the class you are gonna create in a second.
$this->app->singleton(AccountContext::class);
Create the class, where you can set the account in context and get it out.
class AccountContext
{
private $account;
public function getAccount()
{
return $this->account;
}
public function setAccount($account)
{
$this->account = $account;
}
}
Now set your account in the middleware.
$this->middleware(function($req, $next){
resolve(AccountContext::class)->setAccount(Account::find($req->session()->get('account_id')));
return $next($req);
});
Everywhere in your application you can now access the account, with this snippet.
resolve(AccountContext::class)->getAccount();
I'm facing the same problem very often in various projects - I need to set some global variables from database and be able to receive them in anywhere in laravel - views, controllers, models. Is it possible? Or what is the most easy way to do this?
Why I need this? For language translations. I need them not cached and saved in file. For website options which can be taken from any place of app. For website language to set, because I don't want to make /language/ prefix on url.
Sorry if this question can be a duplicate, but none of the answers in similar questions worked in a way I have explained.
You can try view composers for sharing data globally to all views.
public function compose(View $view)
{
//get value from database
$options = Model::where('domain_name', \Request::server("SERVER_NAME"))->get();
//render to view
$view->with('options', $options);
}
The $options variable (model) would be available in every view.
To share data with all controllers define variables in base controller to access them in controllers which inherit base controller
class Controller extends BaseController
{
public $options = Model::where('domain_name', \Request::server("SERVER_NAME"))->get();
}
you can access it using
class LoginController extends Controller
{
public function dashboard()
{
//access here using
$x = $this->options;
}
}
Like this you can create a base model and use the inherit property to access data globally in models.
Hope it will be useful for you. The options variable may contain all the options from database.
Another way to do this is to create a helper class:
1> Add line to composer.json:
"autoload": {
"files": [
"app/Http/helpers.php"
],
2> Create file:
app/Http/helpers.php
3> Add code:
function o($code = null) {
$option = \Option::where('code', $code)->first();
return $option->value;
}
4> Use where you need:
echo o('option_code')
This works in Controller and View, before any render if called. Here can be checked session, config, cookies and etc.
the best way to access a variable everywhere in your project is using sessions. you can store everything in sessions and access it everywhere in your controllers, models and views.
Read the topic: https://laravel.com/docs/5.5/session#using-the-session
Hello everyone as you can tell my still learning, I was wondering how or what is the best method to use in order to reuse my code for database actions ;for instance with the code below
// i reuse this code multiple times throughout my site but everytime I change it I must change
// all of the different Edit's each time I would like a central hub for all of it.
[Authorize]
public ActionResult Edit()
{
var ss = User.Identity.Name;
int myid = Convert.ToInt32(ss);
var buyer = (from s in db.buyers where myid == s.RegistrationID select s).FirstOrDefault();
ViewBag.RegistrationID = myid;
if (buyer != null && buyer.RegistrationID == myid)
{
return View(buyer);
}
else
{
RedirectToAction("buyerrequire");
}
return View("buyerrequire");
}
How can I put such code into a reusable container? so that if I change something there it will change all over that website where that container is used, sortoff like a _Partial except for ActionResults ... thanks for the help..
There are multiple ways of doing that.
1. You can have another base Controller for this purpose as follows:
public class SomeController : Controller{
// the code you use in multiple places.
}
Then the controllers which need these code, can extend this Controller.
You can have an [Attribute] for this functionality and you can decorate the methods with this attribute.
I would go for 2, this is also what asp.net mvc uses for common code such as attributes, logging, security etc.
Exactly how skinny should a controller be? I understand putting all of the business logic inside the models, but what about other things.
For example, say I was writing a blog site where each user can have multiple posts. Currently, the user would create posts by visiting the posts controller and running the create action. Here is a little sample of what would happen currently.
class Controller_Post extends Controller {
function action_create() {
if ( ! empty($_POST)) {
$post = new Model_Post;
$post->user_id = $this->logged_in_user->id;
$post->values($_POST);
if ( ! $post->create()) {
echo 'Error';
}
else
{
echo 'Saved';
}
}
}
}
My question is, what would stop me from putting the above logic in the user model, like so.
class Model_User extends Model {
function create_post($post) {
$post = Model::factory('post')->values($post);
$post->user_id = $this->id;
if ( ! $post->create()) {
return FALSE;
}
else
{
return TRUE;
}
}
}
If it were done this way, the controller would be even smaller than what I put. It makes more sense to me because the user is the one creating the post, so I think it should be in the user model as opposed to the controller.
If it helps, Im using the Kohana framework.
Thanks
Controllers should be directing traffic. Models are for where your business logic goes, so in general your second example would be "correct mvc".
Basically what a controller should be doing is requesting input, telling models to change state (they do the actual state change themselves), and determining which view to display (if any).
I have many controllers which simply are like so:
class Controller_Foobar extends Controller
{
public function action_index() {}
}
And if they need to process $_POST input, they grab that data, and send it off to the model, then the view.
Keeping all that logic inside your models lets you easily reuse it, and it's more maintainable and testable.
I'm using the MVC PHP framework Codeigniter and I have a straight forward question about where to call redirect() from: Controller or Model?
Scenario:
A user navigates to www.example.com/item/555. In my Model I search the item database for an item with the ID of 555. If I find the item, I'll return the result to my controller. However, if an item is not found, I want to redirect the user somewhere. Should this call to redirect() come from inside the model or the controller? Why?
No your model should return false and you should check in your controller like so:
class SampleModel extends Model
{
//Construct
public function FetchItem($id)
{
$result = $this->db->select("*")->from("table")->where("item_id",$id)->get();
if($result->num_rows() == 0)
{
return false;
}
//return result
}
}
and within your controller do:
function item($id)
{
$Item = $this->SampleModel->FetchItem($id);
if(!$Item)
{
redirect("class/error/no_item");
}
}
Models are for data only either return a standard result such as an key/value object or a boolean.
all logic should be handled / controlled by the Controller.
Models are not page specific, and are used globally throughout the whole application, so if another class / method uses the model, it might get redirect to the incorrect location as its a different part of your site.
It seems like the controller would be the best place to invoke your redirect because the controller typically delegates calls to the model, view, or in your case, another controller.
However, you should use whatever makes the most sense for your application and for what will be easier to maintain in the future, but also consider that rules do exist for a reason.
In short, if a coworker were to try to fix a bug in your code, what would the "reasonable person" standard say? Where would most of them be most likely to look for your redirect?
Plus, you said you're returning the result to your controller already... perhaps that's where you should make your redirect...