Initially I have to attach with each action :-
Here we first fetch menu detail then pass in to view section.
Class ManageadministratorController extends Controller {
public $data_menu;
public function __construct() {
$this->middleware('auth');
$obj = new General;
$this->data_menu=$obj->displaymenu();
}
public function index()
{
$obj = new General;
$permission = $obj->checkViewPermission("manageadministrator");
$query= Adminlogin::get();
return View::Make('admin.manageadministrator.manage',array('record'=>$query,'menu_list'=>$this->data_menu));
}
function add()
{
return View::Make('admin.manageadministrator.add',array('menu_list'=>$this->data_menu));
}
}
You can register a custom service provider or use AppServiceProvider:
public function boot()
{
$obj = new General;
$data_menu = $obj->displaymenu();
view()->composer('admin.manageadministrator', function($view) {
$view->with('menu_list', $data_menu);
});
}
Or use a dedicated class:
// app/Providers/AppServiceProvider.php
public function boot()
{
view()->composer('admin.manageadministrator', 'App\Http\Composers\MasterComposer');
}
// app/Http/Composers/MasterComposer.php
use Illuminate\Contracts\View\View;
class MasterComposer {
public function compose(View $view)
{
$obj = new General;
$data_menu = $obj->displaymenu();
$view->with('menu_list', $data_menu);
}
}
Related
I have a method with a lot of code
public function createNewObject(Request $request)
{
// Code...
}
There is another method that I plan to call, but how to pass it to the createNewObject method as a Request argument?
public function deleteAndCreateObject()
{
$this->createNewObject(???);
}
Just type-hint it in your deleteAndCreateObject() method.
class YourController
{
public function createNewObject(Request $request)
{
// Code...
}
public function deleteAndCreateObject(Request $request)
{
$this->createNewObject($request);
}
}
If that—for some reason—doesn't work for you, you can always use request():
class YourController
{
public function createNewObject()
{
$request = request();
// Code...
}
public function deleteAndCreateObject()
{
$this->createNewObject();
}
}
I have a method that accepts a request
public function createUser(Request $request)
{
...
}
I want to call it from another method
public function someMethod(){
$array = [...];
return $this->createUser($array); <----
}
and how can I pass a new request to it with the array I need?
How about instead of trying to call a controller method, you move the logic to create a user to a service class and then use the service class in your createUser and someMethod methods?
UserService.php
class UserService
{
public function __construct() { }
public function createUser(array $userData)
{
// TODO use $userData to create a user here
return $newUser;
}
}
SomeController.php
public function createUser(Request $request)
{
$this->userService->createUser($request->all());
}
public function someMethod(){
$array = [...];
return $this->userService->createUser($array);
}
I have a standard controller, whose logic the same in other controllers. These are admin panel controllers.
<?php
namespace App\Http\Controllers;
use App\Http\Requests\PageRequest;
use App\Page;
use App\Repositories\PageRepository;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
class PageController extends Controller
{
private const REDIRECT_INDEX = 'PageController#index';
protected $model;
public function __construct()
{
$this->model = new PageRepository(new Page());
}
public function index(): View
{
$pages = $this->model->all();
return view('pages.index', compact('pages'));
}
public function create(): View
{
return view('pages.create');
}
public function store(PageRequest $request): RedirectResponse
{
(new Page($request->all()))->save();
return redirect()->action(self::REDIRECT_INDEX)->with('status', 'Created');
}
public function show(Page $page): View
{
return view('pages.show', compact('page'));
}
public function edit(Page $page): View
{
return view('pages.edit', compact('page'));
}
public function update(PageRequest $request, Page $page)
{
$page->fill($request->all())->save();
return redirect()->action(self::REDIRECT_INDEX)->with('status', 'Updated');
}
public function destroy(Page $page): RedirectResponse
{
$page->delete();
return redirect()->action(self::REDIRECT_INDEX)->with('status', 'Deleted');
}
}
In other controllers different only
const REDIRECT_INDEX
$model
vies - 'pages.index', 'pages.create' and etc.
PageRequest $request - request with validation
Page $page - auto finded row from db by slug
So I have PageController, NewsController, TabController, TypeController with the same logic. How can I abstract?
I'm testing my CodeIgniter project with PHPUnit Testing framework (CITest.php). When the function test_model(), calls the model directly to get the details of an user, it works perfectly. But when I do the same via a controller by calling the function test_controller(), it does not output anything (When I debugged, the model itself doesn't gets called). I even verfied if the post data is passed correctly by creating a function test_post_data(). Am I missing something?
I could only find online resources to test the mdoel directly or a controller separately. But I couldn't find any useful link which calls a controller that triggers the model.
CITest.php
class CITest extends PHPUnit_Framework_TestCase
{
private $CI;
public function setUp()
{
$this->CI = &get_instance();
$this->CI->load->model('Test_model');
$this->model = $this->CI->My_model; // load the model
$this->auth = new Test_controller; // load the controller
}
public test_model() {
$user_id = 6;
print_r($this->model->getUserData($user_id));
}
public test_post_data() {
$_POST['useR_id'] = 22;
print_r($this->model->check_post_data());
}
public test_controller() {
$_POST['useR_id'] = 22;
print_r($this->model->get_user_data());
}
}
Test_controller.php
class Test_controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Test_model');
}
public function check_post_data() {
return $this->input->post();
}
public function get_user_data() {
$user_id = $this->input->post('user_id');
return $this->Test_model->getUserData($user_id);
}
}
Test_model.php
class Test_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function getUserData($user_id) {
return $this->db->select("*")
->from("users")
->where("user_id", $user_id)
->get()->result_array();
}
}
The code in CITest.php
public test_controller() {
$_POST['useR_id'] = 22;
print_r($this->model->get_user_data());
}
should be like the following?
public test_controller() {
$_POST['useR_id'] = 22;
print_r($this->auth->get_user_data());
}
I have a model with this code:
<?php
use Illuminate\Database\Eloquent\SoftDeletingTrait;
class Intervention extends Eloquent {
use SoftDeletingTrait;
protected $fillable = array('start_date','stove_id','description','operation_mode','store_id','user_id','intervention_status_id','code');
public function operations()
{
return $this->hasMany('InterventionOperation');
}
public function store()
{
return $this->belongsTo('Store');
}
public function stove()
{
return $this->belongsTo('Stove');
}
public function user()
{
return $this->belongsTo('User');
}
public function statues()
{
return $this->hasMany('InterventionStatus');
}
then the boot
public static function boot()
{
parent::boot();
static::creating(function($intervention)
{
exit("creating");
});
static::created(function($intervention){
exit("created");
});
static::updating(function($intervention)
{
exit("updating");
});
}
the controller:
$intervention = new \Intervention(\Input::all());
$status = \Status::find(\Input::get('status')['id']);
$interventionStatus = new \InterventionStatus();
$interventionStatus->change_status_date = new \DateTime();
$interventionStatus->status()->associate($status);
$interventionStatus->description = "";
$user = \Auth::user();
$store = $user->store;
$intervention->store()->associate($store);
$intervention->user()->associate($user);
$intervention->request_date = new \DateTime();
$intervention->save();
...
but when save model, creating callback is not call.
I have try put exit("test") after parent::boot(); and exit is triggered.
If I put event's code in app/start/global.php it work.
I have try use the code in another model and work.
I do not know why it does not work.
Resolved:
I recreated the database and now everything works. Probably, in the various attempts to save, some relationship was skipped.
Thank you all for the help!
I think this has something to with the namespaces and registering the correct class in the event. Let's hack the source code a bit :)
In: /vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php
Add:
public function getAllEvents()
{
return array_keys($this->listeners);
}
And call/dump Event::getAllEvents();
Try this for both cases (boot in the model and in the global.php) and compare.