issue with php namespace in "Laravel" - php

I use subfolder in the Controller 'folder',which works fine..
but when I write the blow code ..php return the error said "Auth is not found ,and the Input'
<?php
namespace website;
use Auth;
use Input;
use View;
use Illuminate\Routing\Controllers\Controller;
class HomeController extends Controller {
public function index()
{
return View::make('wcsite.index');
}
public function saveHome()
{
$uid = Auth::user()->id;
$websiteData = Input::get('data');
return $uid;
}
}
but when I add 'use Auth,use Input',everything works fine...so ,anyone who can tell me ...is there any way to to this ,which "need not to use Auth,use Input in my subfolder Controllers' Thank you a lot!
and my route is
Route::post('/wcsite',array('uses' => 'website\HomeController#saveHome'))->before('auth');

Your question is a bit confusing. You're saying that the code above is not working because PHP can't find the Auth and Input global class references but your code clearly shows you're importing them correctly.
PHP can't use the global Auth and Input class references without importing them first (which you're doing in the above code). It's going to assume they're located under the website namespace by default.
If you don't want to import hem with use statements you could always reference the global namespace by using a backslash before the class name like the code below:
<?php
namespace website;
use Illuminate\Routing\Controllers\Controller;
class HomeController extends Controller {
public function index()
{
return \View::make('wcsite.index');
}
public function saveHome()
{
$uid = \Auth::user()->id;
$websiteData = \Input::get('data');
return $uid;
}
}
That being said, I prefer importing the classes first instead of using backslashes everywhere. It'll provide for much cleaner code.

Related

Class 'App\Http\Controllers\App\Model' not found

I have a CRUD app, everything works except updating tags
Here is the update function in my controller
namespace App\Http\Controllers;
use App\Tag;
use App\PageList;
use App\PageListTag;
use Illuminate\Http\Request;
public function update(Request $request, $id)
{
$pages = PageList::find($id);
$pages->pagetitle = $request->get('pagetitle');
$pages->articlelist = $request->get('articlelist');
$pages->status = $request->get('status');
$pages->save();
$pages->tags()->saveMany([
new App\Tag(),
new App\Tag(),
]);
return redirect('/pages')->with('success', 'pages updated!');
}
Here is the Tag model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model
{
protected $fillable = ['page_list_id', 'page_list_tag_id'];
protected $with = ['tag'];
public function tag()
{
return $this->belongsTo('App\PageListTag', 'page_list_tag_id', 'id');
}
}
When I run my app I am getting the following error
Class 'App\Http\Controllers\App\Tag' not found
What am I doing wrong in my code?
You're resolving in the wrong way the models namespaces. Please have a look at the official PHP documentation
In your code you're resolving the Tag class as follows
use App\Tag; // <-- This is right
But in your method you're calling
$pages->tags()->saveMany([
new App\Tag(), // <-- And this is wrong!
new App\Tag(),
]);
You simply have to call new Tag() since the use at the top of your file has already included the class.
Otherwise PHP will try to resolve the class from the current namespace. That's why it's throwing
Class 'App\Http\Controllers\App\Tag' not found
To be right you should have added a \ before App\Tag, so PHP will resolve the class from the root. In this case, the use statement will be useless
Your namespace is App\Http\Controllers, so when you create a tag with the syntax new App\Tag() it is indeed translated into App\Http\Controllers\App\Tag.
So just replace your instructions new App\Tag() with new Tag().
Alternatively, you could also use the absolute notation:
new \App\Tag()

Problem of Class not found on Laravel for a form

I tried to follow this tutorial to make a work on Laravel : https://vegibit.com/how-to-set-up-form-submission-in-laravel/ but I got a strange bug and I can't found a way to fix it, I wanted to make a form to insert something in my database and show it in another page (the showing is right, it's just the form page that get this problem) here is a screenshot :
Code :
namespace Doreas\Http\Controllers;
use Illuminate\Http\Request;
use App\Demand;
class DemandeController extends Controller
{
public function index()
{
$demande = Demand::all();
return view('demande.index', ['demande' => $demande]);
}
public function show($id)
{
$demande = Demande::find($id);
return view('demande.show', ['demande' => $demande]);
}
public function create()
{
return 'it works';
}
}
I don't understand where the problem of Demand come from
<?php
namespace App\Doreas;
use Illuminate\Database\Eloquent\Model;
class Demand extends Model
{
public function scopeTest($query)
{
return $query->where('title', '=', 'test');
}
}
Where I declare the class
The problem is that you have imported the class Demand using use App\Demand;
However its namespace is actually App\Doreas so you should do use App\Doreas\Demand;

How to use a public method from a controller to another controller in codeigniter?

I have 2 controllers, Listes.php and Campagnes.php. I want to use a method from the Listes controller in a method from the campagnes, is it possible? And is it possible to pass some parameters to it?
I use Codeigniter 3.
I tried some answers I found here but none of them worked.
I also this in the campagnes.php controller :
include_once (dirname(__FILE__) . "/Listes.php");
class Campagnes extends Listes {
public function listes_recap()
{
$result = parent::add($parameter1, $parameter2);
}
}
and in the Listes.php controller :
class Listes extends CI_Controller {
public function add($parameter1, $parameter2)
{
code here...
}
}
Thanks in advance for you're help.
There are a couple ways to achieve the results you want. But calling one controller from another is NOT the way to go. The "best" way to do it depends on what actually happens in the function that both controllers will use.
The first way is to create a "helper" that each controller will load and then use.
file: /application/helpers/list_add_helper.php
defined('BASEPATH') OR exit('No direct script access allowed');
if ( ! function_exists('add'))
{
function add($parameter1, $parameter2)
{
code here...
}
}
Use it in the controller like this
$this->load->helper('list_add');
$result = add($one, $two);
The second way is to create a custom library (class)
file: /application/libraries/List_adder.php
class List_adder
{
public function add($parameter1, $parameter2)
{
//code here
}
}
Used in any controller
$this->load->library('list_adder');
$result = $this->list_adder->add($one, $two);
If you need to use CI code in your custom library you have a little more work to do. Read all about it HERE.
calling one controller method from another controller is not a good programming strategy

how to protect to delete previous value of class element

I have controller class in laravel in which i have a function create() and a variable attachment i am calling function by ajax
my class code is.
class AttachmentController extends Controller
{
public $_attachments;
public function create()
{
$this->_attachments[]= 'test';
var_dump($this->_attachments);
}
problem is every time when i call it by ajax it return me "test" at 0 index of attachment array . but i want if i call create function 1st time it give me test on 0 index but when next time when i call it . it give me "test" on both 0 and 1 index and so on ..
how it is possible please help me
To preserve the values between requests you need to store them somewhere, an alternative is to use sessions, as the example below, for more information see https://laravel.com/docs/session
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class AttachmentController extends Controller
{
public function create(Request $request)
{
$request->session()->push('attachments', 'test');
var_dump($request->session()->get('attachments'));
}
}
Try something like this.
$_attachments[] = Session::get('test');
$_attachments[] = 'test';
Session::get('test', $_attachments);
dd(Session::get('test'));
let me know if this is what you want.
Since HTTP driven applications are stateless, sessions provide a way
to store information about the user across requests.
class AttachmentController extends Controller
{
public function create()
{
$attachments = Session::get('attachments', array());
$attachments[] = 'test';
Session::put($attachments);
var_dump($this->_attachments);
}
}
Further reading: Laravel Session

Use phalcon classes in base controller rather than on individual controllers

I have a base controller as follows
<?php
use Phalcon\Mvc\Controller;
class ControllerBase extends Controller {
public function initialize() {
}
// wrapper function for debug purposes.
public function pr($data = null) {
echo '<pre>';
print_r($data);
echo '</pre>';
}
}
and a users controller as follows
<?php
use Phalcon\Mvc\Model\Criteria;
use Phalcon\Paginator\Adapter\Model as Paginator;
use Phalcon\Mvc\View;
class UsersController extends ControllerBase {
public function initialize() {
// initialize parent, here ControllerBase.
parent::initialize();
}
public function loginAction() {
// disable the main layout.
$this->view->disableLevel(View::LEVEL_MAIN_LAYOUT);
// disable the controller layout.
$this->view->disableLevel(View::LEVEL_LAYOUT);
}
.
.
.
.
other functions...
}
i was wondering if i could call all the required phalcon classes in base controller and extend then to all the child classes so that i dont need to call them individually on each controller.
in otherwords, can i add the below code
use Phalcon\Mvc\Model\Criteria;
use Phalcon\Paginator\Adapter\Model as Paginator;
use Phalcon\Mvc\View;
only in the base controller and acces them in other controllers. I tried putting them base controller but it gave error : Class not found.
Is this the right way or is there something wrong in my approach...please help.
If I understand your question correctly the answer is NO.
Namespaces are language feature and works this way. The use Phalcon\Mvc\Model\Criteria only declares that you'll use Criteria class from Phalcon\Mvc\Model\ namespace. So in your code you can write new Criteria() to create object instead of using its' full name new \Phalcon\Mvc\Model\Criteria().
You must declare each class in every file which instantiates object of that class so autoloader will know in which file given class exists.

Categories