Codeigniter Model function not working - codeigniter

I am starting out with codeigniter, but the documentation is horribly written and doesn't actually work with the new version. My issue is that I cannot call a function within a model.
Here is my model: User.php
<?php
class User extends CI_Model {
function __construct()
{
parent::__construct();
}
}
function test($x)
{
return $x;
}
?>
And my controller: welcome.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* #see http://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$this->load->model('User');
echo $this->User->test('darn');
$this->load->view('welcome_message');
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
?>

Check your {}s, your test function is outside your User class. Move it inside the class, then $this->User->test() will work.
<?php
class User extends CI_Model {
function __construct()
{
parent::__construct();
}
function test($x)
{
return $x;
}
}
?>
There's nothing "horrible" about the documentation.

Related

Policies Laravel not sending variable to controller

I'm new at Laravel, and I'm trying to make Policies that will prevent user that doesn't have id_level 1 which is admin to access InventarisController, but the InventarisPolicy doesn't send variable to InventarisController.
it's my Inventaris Policies
InventarisPolicy.php
<?php
namespace App\Policies;
use App\{User, Level};
use Illuminate\Auth\Access\HandlesAuthorization;
class InventarisPolicy
{
use HandlesAuthorization;
/**
* Create a new policy instance.
*
* #return void
*/
public function __construct()
{
//
}
public function inventaris_add(User $user)
{
$user->id_level == 1;
// dd($user);
// $user->id_level == 2;
}
}
it's my Inventaris Controller
InventarisController.php
<?php
namespace App\Http\Controllers;
use App\{Inventaris, DetailPinjamanView};
// use Illuminate\Http\Controllers\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
// use App\Http\Controllers\Auth\Request;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class InventarisController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
// $viewpinjaman = DetailPinjamanView::all();
$this->authorize('inventaris_add', $user);
$inventaris = Inventaris::all();
return view('index', compact('inventaris'));
}

laravel authentication redirect

I'm new to Laravel and have just added the Authentication package to an existing project.
Upon logging in, I want to be redirected to /Result a page that that I know works using a controller. If I type the URL /Result the page loads correctly but when I login I am being redirected to index each time rather than /Result
Routes
Route::get('/result','ResultsController#getResults')->name('result');
Auth::routes();
Route::get('/', 'HomeController#index')->name('/');
Home Controller
class HomeController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('result');
}
}
Results Controller
class ResultsController extends Controller
{
public function getResults( )
{
$results = Result::all();
return view('/result', ['results' => $results]);
}
}
Login Controller
class LoginController extends Controller
{
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* #var string
*/
protected $redirectTo = 'result';
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
/**
* #return
*/
public function authenticated()
{
return redirect()->route('result');
}
}
So far I can load index and be redirected to login, when I login I want to be redirected to /Result but instead I recieve an Undefined variable: results.
I have jumped to /Results by manipulating the URL and the page /Results does work.
Any help would be much appreciated, just le me know if you need any additional code examples from any other files.
thanks
James
First of all change the route name format use only result.
Route::get('/result','ResultsController#getResults')->name('result')
For redirect any route you can use LoginController authenticate method.
\App\Http\Controllers\Auth\LoginController.php
Add this method to that controller:
/**
* #return
*/
public function authenticated()
{
return redirect()->route('result');
}

Codeigniter Dompdf : Call to a member function isHtml5ParserEnabled() on null

I placed the 'dompdf' # '/system/libraries/', and create a class file 'Dompdf.php' at the same dir, code as below:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once("dompdf/autoload.inc.php");
use Dompdf\Dompdf;
use Dompdf\Options;
class CI_Dompdf extends Dompdf {
/**
* Set the template from the table config file if it exists
*
* #param array $config (default: array())
* #return void
*/
public function __construct($config = array())
{
log_message('info', 'Dompdf Class Initialized');
}
}
below is part of the controller function which using the dompdf library:
public function pdf_create($html, $filename='', $stream=TRUE){
$this->load->library('Dompdf','dompdf');
$this->dompdf->load_html($html);
$this->dompdf->render();
if ($stream) {
$this->dompdf->stream($filename.".pdf");
} else {
return $this->dompdf->output();
}
}
but I get the error below:
PHP Fatal error: Call to a member function isHtml5ParserEnabled() on null in /home/mysite/public_html/ci/system/libraries/dompdf/src/Dompdf.php on line 492
I'd checked the function does exists in 'dompdf/src/Options.php'.
I have no idea how to solve it.
My dev env:
Codeigniter 3.0.6
Dompdf 0.7.0
Thanks.
UPDATE Found error occurred due to not call Dompdf constructor, correct code as below:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once("dompdf/autoload.inc.php");
use Dompdf\Dompdf;
class CI_Dompdf extends Dompdf {
/**
*
* #param array $config (default: array())
* #return void
*/
public function __construct($config = array()) {
parent::__construct($config);
log_message('info', 'Dompdf Class Initialized');
}
}

how to get current logged-in user details in laravel 5.2?

how do i get current logged in user details in laravel 5.2 ? I have done something to get the user name but it doesn't work properly.
Here is the code that gets the current logged-in user name
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
use DB;
use Illuminate\Contracts\Auth\User;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Auth\AuthController;
class UserProfileController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function current_user()
{
if(\Auth::check() && \Auth::user()->name) {
return 'hello';
return user()->name;
}
}
}
Here If i just return a ** return 'Hello'** in the function current_user , it works fine after checking the if condition, so i see there is no problem with if condition. But when i return user()->name it says
Call to undefined function App\Http\Controllers\user()
To access authorized user information use \Auth::user(). You probably just missing \Auth:: part, because You using correct syntax in condition.
You need to change your code a little bit.
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
use DB;
use Illuminate\Contracts\Auth\User;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Auth\AuthController;
class UserProfileController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function current_user()
{
if(\Auth::check() && \Auth::user()->name) {
return 'Hello '.\Auth::user()->name; //this line changed
}
}
}
After that you can get the current user name.
You want to return 2 values, that's not possible. When you return something your method will stop running. So that's why you didn't get the user name.
Hope this works!

How to send id to controller in codeigniter

I'm using below code for my site, but it displayed me
404: Page not found.
routes
$route['class_name/function_name/(:num)'] = 'class_name/function_name/$1';
Controller.
public function function_name($Id)
{
print_r($Id); exit;
}
Use URI to extract the id from URL:
Following is the link in docs : http://www.codeigniter.com/userguide2/libraries/uri.html
Example- How to get the a URL using uri_string() codeigniter?
I have create a simple one maybe it could help you out
this is my controller welcome.php fresh from codeigniter
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* #see http://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$this->load->view('welcome_message');
}
public function test1($ID) {
echo $ID;
}
public function test2($ID, $ID2) {
var_dump($ID, $ID2);
}
public function test() {
echo 'test';
}
}
and here is my route
$route['default_controller'] = 'welcome';
$route['welcome/test1/(:num)'] = 'welcome/test1/$1';
$route['welcome/test/(:num)/(:num)'] = 'welcome/test/$1/$2';
example to access it if working
https://192.168.248.209/stackoverflow/welcome/test2/1/3
https://192.168.248.209/stackoverflow/welcome/test1/1
this one is dynamic according to your server implementation
See Pretty Url Setup CodeIgniter

Resources