My project worked fine on localhost, but once it was uploaded to a Linux shared host, I got the following error.
Class 'App\Application\Model\Slider' not found
This problem occurred in three of my models: sections, projects, Slider. The other models work fine.
HomeController.php
namespace App\Application\Controllers;
use App\Application\Model\Page;
use App\Application\Model\projects;
use App\Application\Model\section;
use App\Application\Model\Slider;
class HomeController extends Controller
{
public function __construct()
{
$this->middleware('auth')->except(['getPageBySlug', 'welcome']);
}
public function index()
{
return view('website.home');
}
public function getPageBySlug($slug)
{
$page = Page::where('slug', $slug)->first();
if ($page) {
return view('website.page', compact('page'));
}
return redirect('404');
}
public function welcome()
{
$sections = \App\Application\Model\section::limit(3)->orderBy('id')->get();
$project = \App\Application\Model\projects::limit(3)->orderBy('id')->get();
$sliders = \App\Application\Model\Slider::get();
return view('website.welcome', compact('projects', 'sections', 'sliders'));
}
}
Namespaces are loaded through an autoload file. When you push to your shared hosting, you'll likely have to run 'composer dump-autoload' in the project root to compile this file.
Related
I am creating model and I want to access method in controller, but query gives error:
mysqli_sql_exception #1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ':0: AND 1 = :1: LIMIT 1' at line 3
What is mistake in my code?
<?php
namespace App\Models;
use CodeIgniter\Model;
class UserModel extends Model{
protected $table='users';
protected $allowedFields=['employee_name','employee_email','employee_phone','employee_dept','employee_grade','employee_password','employee_cpassword','emp_image'];
public $db;
public function __construct()
{
$this->db = \Config\Database::connect();
}
public function getEmployee($id=false)
{
if($id==null)
{
return $this->findAll();
}
return $this->where(['id',$id])->first();
}
}
Controller:
public function edit($id) {
$model = model(UserModel::class);
$data['emp']=$model->getEmployee($id);
print_r($data['emp']);
exit();
}
Routes:
$routes->get('/edit/(:num)','Admin::edit/$1');
Change from:
return $this->where(['id',$id])->first();
to
return $this->where('id', $id)->first();
or
return $this->where(['id' => $id])->first();
Take a look at the document here
I would like to go to specific object view from main page(index). Here is how I communicate with page which is about to be showed
{{ route('object',['id'=>$object->id]) }}
Error message:
Error message says that it should be something wrong with controller, but in previous case this code worked.
web.php(routes)
Route::get('/object/{id}','FrontendController#object')->name('object');
My FrontendController
use App\Interfaces\FrontendRepositoryInterface;
class FrontendController extends Controller
{
public function __construct(FrontendRepositoryInterface $frontendRepository)
{
$this->fR = $frontendRepository;
}
public function index()
{
$objects = $this->fR->getObjectsForMainPage();
//dd($objects);
return view('frontend.index',['objects'=>$objects]);
}
public function object($id)
{
$object = $this->fR->getObject($id);
//dd($objects);
return view('frontend.object',['object'=>$object]);
}
FrontendRepository
use App\Models\EventObject;
use App\Models\Photo;
use App\Interfaces\FrontendRepositoryInterface;
class FrontendRepository implements FrontendRepositoryInterface {
public function getObjectsForMainPage()
{
return EventObject::with(['photos','clubs'])->get();
}
public function getObject($id)
{
return EventObject::with(['photos','clubs'])->get();
}
}
Solution was to clear routes cache using php artisan route:cache and to add missing / in route
Route::get('/object/'.'{id}','FrontendController#object')->name('object');
I am creating a Composer package, beginner
Via Composer I have added my package to test application.
The test application output should be 12345, but instead it prints view name
What is wrong?
DownloadsServiceProvider:
class DownloadsServiceProvider extends ServiceProvider
{
public function boot()
{
$this->loadViewsFrom(__DIR__.'/resources/views/download', 'downloads');
public function register()
{
new Routes();
}
}
Controller:
namespace xxx\xxx\downloads;
use Illuminate\Http\Request;
class DownloadsController
{
public function index(Request $request)
{
$test='12345';
return View('downloads::test' ,compact('test'));
}
}
Route:
public function registerRoutes()
{
Route::prefix(self::CMSURL)->group(function () {
Route::get(
'/downloads',
function (Request $request) {
//return (new DownloadsController())->index($request)->name(self::DOWNLOADS_LISTER);
return (new DownloadsController())->index($request);
}
);
});
}
View:
{{$test}}
12345
Output source:
downloads::test
I am trying to get started using controllers in Laravel 4 and I am running into some trouble. Here is the basic run down:
I have a controller in the controllers folder called FansController in the file FansController.php:
<php
class FansController extends BaseController {
public $restful = true
public function getindex() {
return View::make('fans.landing');
}
}
Within my views folder, I have a folder called "fans" that controls a view file "landing.blade.php". It contains simple html: <h1>hello</h1>
In my routes.php file, I have a route calling the controller. Here is that code:
Route::get('landing', array('uses' => 'FansController#index'));
When I visit the url: public/fans/landing
I receive a "NotFoundHttpException";
Do you have any ideas what may be going wrong? Thank you for your help.
First off you need to specify what version of Laravel you are running.
If you are running Laravel 4 then:
// routes.php
Route::get('/', array('uses' => 'GuestController#getIndex'));
// GuestController.php
class GuestController extends BaseController {
public function getIndex() {
return 'Hello world.';
}
}
Then run $ composer dump-autoload -o or php composer.phar dump-autoload -o (if your composer is installed locally) on your CLI
In laravel 3, however
// routes.php
Route::get('/', array('uses' => 'GuestController#index'));
// GuestController.php
class GuestController extends BaseController {
public $restful = true;
public function get_index() {
return 'Hello world.';
}
}
Probably you need to change your method name to index:
class FansController extends BaseController {
public $restful = true
public function index() {
return View::make('fans.landing');
}
}
My controller can connect to the DB but my model can not. I have autoloaded the DB in the autoload.php file, but no luck in the model.
so if I do something like
$this->db->insert('table', $data);
I receive this Call to a member function insert() on a non-object I have used Codeigniter before but have never had this issue, on my other project I did not even use parent::__construct()
class Bucketlist extends CI_Model {
private $data = array();
public function __construct(){
parent::__construct();
}
// Setter Function
public function __set ($var, $val) {
$this->data[$var] = $val;
}
// Getter Function
public function __get($var) {
return (isset($this->data[$var])) ? $this->data[$var] : null;
}
// Create WishList
function createBucketList($bucketlist) {
$this->db->insert('_bucketlist', $bucketlist->data);
}
}
thanks.
You may need to Autoload the Database connection (http://ellislab.com/codeigniter/user-guide/database/connecting.html), as it appears that the db variable is not instantiated before you are trying to use it.