I'm working on Laravel framework, I have created a class under controllers/admin/PlacesController.php
I'm placing it in a name space controller/admin;
But as you can see below I can't use standart Laravel classes without "\"
Please see \View
class PlacesController extends \BaseController {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
$places=\Place::all();
return \View::make('admin.places.index',compact('places'));
}
/**
* Show the form for creating a new resource.
*
* #return Response
*/
public function create()
{
return \View::make('admin.places.createOrEdit');
}
}
But I would want to use it as View without "\" how can I do this? It is really a problem to fix all View to \View
Thanks all.
You should import View class because it's in other namespace (root namespace).
Add:
use View;
at the beginning of your file, for example:
<?php
namespace yournamespacehere;
use View;
Now you will be able to use in your controllers return View instead of return \View
If you want more explanation you could look at How to use objects from other namespaces and how to import namespaces in PHP
Related
I'd like to add a table in resources\views\welcome.blade.php. So I add the html table code there and it works.
Then I try to edit app\Http\Controllers\HomeController.php to see a vardump in welcome.blade.php but nothing shows there, continue only my table.
Any ideas what I'm doing wrong? I'm new using laravel.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Model\ModelHome;
use App\User;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
$this->objUser=new User();
$this->objBook=new ModelFuncionarios();
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
dd($this->objUser);
return view('home');
}
}
Your Homecontroller doesn't use welcome.blade.php but home.blade.php. dd(...) is short for "dump & die" - it dumps the information and stops script execution.
Also, you need to be logged in/auth'd to use HomeController, you are redirected to welcome.blade.php before executing your index method.
Edit: if you want to "var_dump" information and not stop script execution but continue to show the view you can use dump() instead of dd()
When you do dd() the view will not load as this function ends the execution of the script.
Have a look here: laravel 8 dd function
I am using Laravel 5.8 and attempting to set up a custom validation extension.
I have created a class GroupValidator containing a validate function.
I have created a ValidationServiceProvider with the following code:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Validator;
use App\Classes\GroupValidator;
class ValidationExtensionServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* #return void
*/
public function register()
{
//
}
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
Validator::extend('valid_parent_id', 'GroupValidator#validate');
}
}
When my validation triggers I get a Class GroupValidator does not exist exception. However if I specify the full path to my class in the extend function call like so:
Validator::extend('valid_parent_id', 'App\Classes\GroupValidator#validate');
then everything works fine.
Is there some way I can set this up so that I don't have to include the full path to my class?
I am wondering how to register custom method within:
Illuminate/Foundation/Application.php
Like:
/**
* Get the current application name.
*
* #return string
*/
public function getName()
{
return $this['config']->get('app.name');
}
Taken from:
/**
* Get the current application locale.
*
* #return string
*/
public function getLocale()
{
return $this['config']->get('app.locale');
}
Where should I put this, instead of vendor file ofc?
Thanks
You maybe able to extend the Illuminate/Foundation/Application.php
example:
<?php
namespace app\Custom;
class Application extends \Illuminate\Foundation\Application
{
}
for more details, please refer this click here .
Note: Adding your code under vendor files, may lost during the composer update command. So its not advisable to actually add your custom codes there.
Using Laravel 5.6, I'm trying to get the number of received links a logged-in user may have in my application.
public function getReceivedLinksCount() {
return $count = App\Link::where([
['recipient_id', Auth::id()],
['sender_id', '!=', Auth::id()]
])->count();
}
So far, so good. Question is not about that piece of code, but where I can use it. I'd like to display this counter on the navigation bar of the website (Facebook's style) which is in my header.blade.php which is included in every page.
I'd like to do it with a clean code, of course. It seems like I need to use View Composers but I'm not sure it's the right way to do it, and not sure on how the code is supposed to look.
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
view()->composer('layouts.header', function ($view) {
$view->with('variable_name', \App\Path-To-Your-Model::something());
});
}
You can share a value across all views by using View::share(), see docs
For example
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
$linksCount = Link::getReceivedLinksCount();
View::share('linksCount', $linksCount);
}
...
}
This works well if you want to set the value everywhere. Personally, I would set the value in the constructor of a 'BaseController' that gets extended by other controllers. This makes the code more discoverable because most people would expect view values to be set in a controller. And it's also a bit more flexible if you plan on having a section of your app that doesn't require that value to be computed.
I am working in laravel 5.5. I have created a controller folder with name Abc and inside it created a controller file with name abc.php, inside this file I have written code:
namespace App\Http\Controllers\Abc;
use App\Http\Controllers\Controller;
//use Illuminate\Support\Facades\DB;
class abcController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function NewPromotion()
{
return view('new-promotion');
}
}
And in Route folder under web.php file I am calling its view file, like:-
Route::get('/new-promotion','Abc\abcController#NewPromotion');
In spite of defining in namespace I am still getting error i.e.
"ReflectionException (-1)
Class App\Http\Controllers\Abc\abcController does not exist".
What could be the possible issue?
You should make sure your file is located in app/Http/Controllers/Abc directory and has name abcController.php (case sensitive).
Also by convention class names start with big letter, so you should rather name your controller AbcController and have this file with name AbcController.php (and also update your route file to use valid case of this controller name).
Good way to create controller is use cmd (you have to be in project directory)
php artisan make:controller "Abc\abcController"