<?php
namespace Laravel\Horizon\Http\Controllers;
class HomeController extends Controller
{
/**
* Single page application catch-all route.
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('horizon::app'); // what's the meaning of this 'horizon::app'
}
}
I found this syntax in the Laravel-Horizon Controller, can anyone explain this:
view('horizon::app');
What is the meaning of 'horizon::app'?
Like others answers stated, that is known as view namespaces. It not limited to the package's view, but you can use it inside your project as well.
As example you might have admin and customer module and want to differentiate their view by their own folder name, at this point you could use the namespace declaration. As example you might have these folders structures:
|- resources
|- views
|- admin
|- index.blade.php
|- customer
|- index.blade.php
Then you could register your own namespace that point to that particular folder path in AppServiceProvider.php:
app('view')->addNamespace('admin', base_path() . '/resources/views/admin');
// or
app('view')->addNamespace('customer', base_path() . '/resources/views/customer');
And later on, inside controller's method, you can reference it using:
return view("admin::index");
// or
return view("customer::index");
This syntax indicates that the View named app belongs to the horizon package. Think of it as package::view.path.
More info in Laravel's Package Development documentation.
To register your package's views with Laravel, you need to tell Laravel where the views are located.
Package views are referenced using the package::view syntax convention. So, once your view path is registered in a service provider, you may load the admin view from the courier package like so:
Route::get('admin', function () {
return view('courier::admin');
});
This feature used to be called view namespaces, if you've seen that term or want something else to search for. :)
:: is the scope (namespace) operator. Meaning app is declared within horizon.
Example (from php.net):
<?php
class MyClass {
const CONST_VALUE = 'Un valor constante';
}
$classname = 'MyClass';
echo $classname::CONST_VALUE; // A partir de PHP 5.3.0
echo MyClass::CONST_VALUE;
?>
Related
I have installed the Laravel in sub-folder and is trying to install the horizon. After routing to "test.com/sub-folder/horizon", all the design in broken and also the internal links are pointing to main domain instead of main-domain-without-subfolder.
After the search, it seems to be the known issue which is already reported in github issue
Has there is any work around to make horizon work when Laravel is installed in sub-folder?
I have a solution that only involves PHP.
The issue, as pointed out by #Isaiahiroko, is the basePath defined for Horizon's interface. That code is in Laravel\Horizon\Http\Controllers\HomeController::index(). The idea is this: we are going to pass to Laravel's service container our own implementation of that controller that will override the basePath definition passed to Horizon's interface.
Create a new controller with code like this:
<?php
namespace App\Http\Controllers;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Str;
use Illuminate\View\View;
use Laravel\Horizon\Horizon;
use Laravel\Horizon\Http\Controllers\HomeController;
class HorizonHomeController extends HomeController
{
/**
* Overrides default horizon route to support subdirectory hosting.
*/
public function index ()
{
// We use a plain request to check for the base url.
$request = request();
// Set up our base path.
$base_path = Str::substr($request->getBasePath(), 1);
if (!empty($base_path)) {
$base_path .= '/';
}
// Patch default horizon variables with our own base path.
$variables = Horizon::scriptVariables();
$variables['path'] = $base_path . config('horizon.path');
// Render horizon's home view.
return view('horizon::layout', [
'assetsAreCurrent' => Horizon::assetsAreCurrent(),
'horizonScriptVariables' => $variables,
'cssFile' => Horizon::$useDarkTheme ? 'app-dark.css' : 'app.css',
'isDownForMaintenance' => App::isDownForMaintenance(),
]);
}
}
What's left is telling Laravel's service container that when Horizon's HomeController is requested, it should provide our HorizonHomeController class. In your AppServiceProvider, at the end of the register() method, set this up:
// [...]
class AppServiceProvider extends ServiceProvider
{
// [...]
/**
* Register any application services.
*
* #return void
* #throws InvalidConfiguration
*/
public function register()
{
// [...]
// Horizon's subdirectory hack
$this->app->bind(
Laravel\Horizon\Http\Controllers\HomeController::class,
App\Http\Controllers\HorizonHomeController::class
);
}
// [...]
}
After that, you should be able to browse to http(s)://<your-host>/<your-sub-dir>/horizon normally.
Considerations:
To me this feels cleaner that patching a compiled js, which also has the downside that needs to be re-applied every time Horizon is updated (this can be mitigated with a post-update script in composer, tho). Also, for additional points, this solution is only overriding the method that renders the view, but not the route, which means all of Horizon's authentication mechanisms (middlewares and gates) are working exactly as described in the documentation.
If you desperately need to do this, here is a hack:
In public\vendor\horizon\app.js, search for window.Horizon.basePath
replace window.Horizon.basePath="/"+window.Horizon.path; with window.Horizon.basePath="/[you sub-directoy]/"+window.Horizon.path;
It should work...until you run update one day and it mysteriously stop working.
How i can define blade directory in liravel ?
I wanna to use:
return view('panel::post.create');
// e.g : resurces/views/vendor/panel/post/create.blade.php
How i should define vendor/panel as panel:: ?
You may do this using the service provider's loadViewsFrom method. The loadViewsFrom method accepts two arguments: the path to your view templates and your package's name. For example, if your package's name is panel, you would add the following to your service provider's boot method:
public function boot()
{
$this->loadViewsFrom(__DIR__.'/path/to/views', 'panel');
}
Package views are referenced using the package::view syntax convention. So, once your view path is registered in a service provider, you may load the admin view from the panel package like so:
Route::get('admin', function () {
return view('panel::admin');
});
Read more at official Laravel documentation.
I have two controller file homecontroller and backendcontroller. What is the best way to create global function and access it from both files?
I found here Arian Acosta's answer helpful but I wonder if there is an easiest way. I would appreciate any suggestions.
Solution
One way to do this is to create a class and use its instance, this way you can not only access the object of the class within a controller, blade, or any other class as well.
AppHelper file
In you app folder create a folder named Helpers and within it create a file name AppHelper or any of your choice
<?php
namespace App\Helpers;
class AppHelper
{
public function bladeHelper($someValue)
{
return "increment $someValue";
}
public function startQueryLog()
{
\DB::enableQueryLog();
}
public function showQueries()
{
dd(\DB::getQueryLog());
}
public static function instance()
{
return new AppHelper();
}
}
Usage
In a controller
When in a controller you can call the various functions
public function index()
{
//some code
//need to debug query
\App\Helpers\AppHelper::instance()->startQueryLog();
//some code that executes queries
\App\Helpers\AppHelper::instance()->showQueries();
}
In a blade file
Say you were in a blade file, here is how you can call the app blade helper function
some html code
{{ \App\Helpers\AppHelper::instance()->bladeHelper($value) }}
and then some html code
Reduce the overhead of namespace (Optional)
You can also reduce the overhead of call the complete function namespace \App\Helpers by creating alias for the AppHelper class in config\app.php
'aliases' => [
....
'AppHelper' => App\Helpers\AppHelper::class
]
and in your controller or your blade file, you can directly call
\AppHelper::instance()->functioName();
Easy Solution:
Create a new Helpers folder in your app directory.
Create a php file named your_helper_function.php in that Helpers directory.
Add your function(s) inside your_helper_function.php
function your_function($parameters){
//function logic
}
function your_another_function($parameters){
//function logic
}
Add this file to the Files key of your composer.json like
"autoload": {
...
"files": [
"app/Helpers/your_helper_function.php"
]
...
}
Finally, regenerate composer autoload files. (Run this in your project directory)
composer dump-autoload
That's it! and now you can access your_function() or your_another_function() in any part of your Laravel project.
If you still have any confusion, check my blog post on how to do this:
How to Add a Global Function in Laravel Using Composer?
Updated:
Step 1
Add folder inside app folder
app->Helper
Step 2
add php Class inside Helper folder
Eg. Helper.php
Add namespace and class to the Helper.php
namespace App\Helper;
class Helper
{
}
Register this Helper.php into config/app.php file
'aliases' => [
....
'Helper' => App\Helper\Helper::class
]
Now, write all the functions inside Helper.php and it will be accessible everywhere.
How to access from Controller?
Step 1 - Add a namespace at top of the controller.
use App\Helper\Helper;
Step 2 - Call function - Assume there a getInformation() inside the Helper Class.
$information = Helper::getInformation()
In your Controller.php which extends BaseController, you can create a function like;
public function data($arr = false)
{
$data['foo'] = 'bar';
return array_merge($data,$arr);
}
And from any controller when you send a data to a view;
public function example()
{
$data['smthg'] = 'smthgelse';
return view('myView',$this->data($data));
}
The data in the the main controller can be accessed from all controllers and blades.
The Laravel Service Provider way
I've been using global function within Laravel for a while and I want to share how I do it. It's kind of a mix between 2 answers in this post : https://stackoverflow.com/a/44021966/5543999 and https://stackoverflow.com/a/44024328/5543999
This way will load a file within a ServiceProvider and register it within your Laravel app.
Where is the difference, the scope, it's always about the scope.
Composer //Autload whitin composer.json method
|
|--->Laravel App //My method
|
|--->Controller //Trait method
|--->Blade //Trait method
|--->Listener //Trait method
|--->...
This is a really simplist way to explain my point, all three methods will achieve the purpose of the "Global function". The Traits method will need you to declare use App\Helpers\Trait; or App\Helpers\Trait::function().
The composer and service provider are almost about the same. For me, they answer better to the question of what is a global function, because they don't require to declare them on each place you want to use them. You just use them function(). The main difference is how you prefer things.
How to
Create the functions file : App\Functions\GlobalFunctions.php
//App\Functions\GlobalFunctions.php
<?php
function first_function()
{
//function logic
}
function second_function()
{
//function logic
}
Create a ServiceProvider:
//Into the console
php artisan make:provider GlobalFunctionsServiceProvider
Open the new file App\Providers\GlobalFunctionsServiceProvider.php and edit the register method
//App\Providers\GlobalFunctionsServiceProvider.php
public function register()
{
require_once base_path().'/app/Functions/GlobalFunctions.php';
}
Register your provider into App\Config\App.php wihtin the providers
//App\Config\App.php
'providers' => [
/*
* Laravel Framework Service Providers...
*/
Illuminate\Auth\AuthServiceProvider::class,
...
Illuminate\Validation\ValidationServiceProvider::class,
Illuminate\View\ViewServiceProvider::class,
App\Providers\GlobalFunctionsServiceProvider::class, //Add your service provider
Run some artisan's commands
//Into the console
php artisan clear-compiled
php artisan config:cache
Use your new global functions
//Use your function anywhere within your Laravel app
first_function();
second_function();
Laravel uses namespaces by default. So you need to follow the method described in that answer to setup a helper file.
Though in your case you want to access a method in different controllers. For this there's a simpler way. Add a method to you base controller app/Http/Controllers/Controller.php and you can access them in every other controller since they extend it.
// in app/Http/Controllers/Controller.php
protected function dummy()
{
return 'dummy';
}
// in homecontroller
$this->dummy();
There are a few ways, depending on the exact functionality you're trying to add.
1) Create a function inside Controller.php, and make all other controller extend that controller. You could somewhat compair this to the master.blade.php
2) Create a trait, a trait can do a lot for you, and keeping ur controllers clean. I personally love to use traits as it will look clean, keep my Controller.php from being a mess with tons of different lines of code.
Creating a global function
create a Helpers.php file under a folder, let's name it 'core'.
core
|
-- Helpers.php
namespace Helpers; // define Helper scope
if(!function_exists('html')) {
function html($string) {
// run some code
return $str;
}
}
In your composer.json
"autoload": {
"psr-4": {
},
"files": [
"core/Helpers.php"
]
}
in the file that you want to use it
// the " use " statement is not needed, core/Helpers is loaded on every page
if(condition_is_true) {
echo Helpers\html($string);die();
}
Remove the namespace in Helpers.php if you want to call your function without the need to prefix namespace. However I advise to leave it there.
Credit: https://dev.to/kingsconsult/how-to-create-laravel-8-helpers-function-global-function-d8n
By using composer.json and put the function containing file(globalhelper.php) to the autoload > files section, then run
composer dump-autoload
You can access the function inside the file(globalhelper.php) without having to calling the class name, just like using default php function.
I'm trying to share an object across a Laravel application. I need this because I want to create a blade template which will be included everywhere and will also perform some logic/data manipulation (a dynamic menu sort of speak).
To be able to accomplish this I've created a constructor in the Base controller and used View::share facade.
While this works across all routes in the application, it's not working for Zizaco/Confide generated routes, where I get Undefined variable error for $books.
This is the constructor in the base controller:
public function __construct()
{
$books = Book::all();
View::share('books', $books);
return View::make('adminMenu')->with('books', $books);
}
What you need are View Composers!!
You can hook a view composer to a certain view name or pattern (using * wildcard). Every time before that view gets rendered the view composer will run.
You can put this anywhere. Most elegant would be a custom app/composers.php which is then required at the bottom of app/start/global.php
View::composer('adminMenu', function($view){
$books = Book::all();
$view->with('books', $books);
}
The structure looks like this:
modules
admin
controllers/
admin.php
models/
admin_model.php
views/
admin/
index.php
categories/
controllers/
admin.php
categories.php
models/
categories_model.php
views/
admin/
index.php
menu.php
frontpage.php
posts/
controllers/
admin.php
posts.php
models/
posts_model.php
views/
admin/
index.php
menu.php
frontpage.php
The admin controller looks like:
class Admin extends Backend_Controller {
function __construct()
{
parent::__construct();
$this->load->model('categories_model');
}
public function index()
{
// index stuff
}
public function _menu()
{
$this->load->view('categories/admin/menu');
}
}
And when I am calling it from another module view like this:
<?php echo Modules::run('categories/admin/_menu'); ?>
it doesn't work ;(
However this works:
<?php echo Modules::run('categories/categories'); ?>
So my problem is how to load the controller with a name admin and not the name as the module's name and the method "menu"
Any idea how could I make it work in CodeIgniter?
EDIT:
I have found out that if I change my controller name from "admin" to something else e.g. "blablacontroller" it magically starts working.
I have already another module called "admin" so could this be a problem?
If I'm right, you are using Modular Extensions - HMVC. So I've based my answer on the following script : https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/src/868e97533562e910d8263af22750985d57004baa/third_party/MX/Modules.php?at=default.
This will only work if you are using PHP 5.3 or above.
Declare every Admin classes inside a namepsace (admin, categories, post) by adding namespace admin; before the class keywork.
Create a child class of *third_party/MX/Modules.php* and extend the run and load methods. If extending is not possible, you'll have to replace the methods :/
This is not the whole thing, but those simples string operations should be in the load method before the existing logic.
A module should be run with the following syntax : *Modules::run('categories\admin/_menu');*
$module = 'categories\admin';
if (strpos($module, '\\') !== false)
{
// Extract namespace
$ns = explode('\\', $module);
// Get the top level namespace to locate the controller
$top_level_ns = reset($ns); //
// Remove namespace from module
$module = array_pop($ns);
// Class location
$path = APPPATH . $top_level_ns . '/controllers/' . $module;
// It's better to extend the load_file method
include_once $path . EXT;
// Full class name with namespace, we use explode on $module in case of sub folders
$controller = '\\' . implode('\\', $ns) . '\\' . ucfirst(end(explode('/', $module))) . CI::$APP->config->item('controller_suffix');
// create and register the new controller
$alias = strtolower($controller);
self::$registry[$alias] = new $controller($params);
var_dump('Full class name: ' . $controller, 'Class path: ' . $path);
}
In PHP you can only have a single class declared with a name. Eg. you can only have a single Admin class unless the classes are in different namespaces. What might be happening is:
admin/controllers/admin.php is declared then later in your scripts execution categories/controllers/admin.php is attempted to be declared and throws an error as admin/controllers/admin.php already exists and which Admin class should it use if some code somewhere says new Admin().
If both classes are needed then one will need to renamed, or the code to be restructured so that only one of them is declared in a single execution cycle. I am not too sure if the HMVC stuff your using will allow namespaces, but you can look at extending it that way. If not maybe rename both admin classes to something a bit more specific.