How to use the Worpress session outside folder in Laravel - laravel

I am facing trouble to get the current user login detail in Laravel from WordPress
My Directory Structure
/public_html/
/public_html/ here is all Laravel files like app, route, resources etc.
/public_html/shop/ here is the all WordPress Files
I was using the Service provider for load the wp-load.php files but when I call it its through me error i.e
Cannot redeclare __() (previously declared in /public_html/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:851)
WordPressServiceProvider
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use URL;
class WordPressServiceProvider extends ServiceProvider
{
/**
* Path to our WP installation
*
* #var string
*/
protected $bootstrapFilePath = '..\shop\wp-load.php';
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
//echo 'SESSION => '.is_user_logged_in();
/**
* Here we can enqueue any extra scripts or styles we may need.
* I loaded my frontend specific styles and scripts that were of no use to the WP site
*/
//wp_enqueue_style('frontend-styles', URL::asset('css/frontend.css'), ['dependencies'], false);
//wp_enqueue_script('frontend-scripts', URL::asset('js/frontend.js'), ['dependencies'], false, true);
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
if(\File::exists($this->bootstrapFilePath)) {
//echo 'INSIDE REGISTER';
require_once($this->bootstrapFilePath);
}else{
//echo 'OUTSIDE REGISTER'.$this->bootstrapFilePath;
}
}
}

Related

Laravel using loadViewComponentsAs method from SurviceProvider without creating the package

When installing jetstream with it's authentication/authorization system laravel developer receive scaffolding with lots of views and view-components.
Firstly those views are in vendor\laravel\jetstream\resources\views\components\ folder.
Then when developer publishes views according to jeststream installation: https://jetstream.laravel.com/1.x/installation.html#livewire-components
php artisan vendor:publish --tag=jetstream-views
Those views/components appears at resources\views\vendor\jetstream\components.
The links to those views on the blade templates are:
<x-jet-welcome />
or to unify
<x-jet-someJetElement />
This corresponds with laravel blade documentation: https://laravel.com/docs/8.x/packages#view-components
We need to register those aliases in SurviceProvider:
public function boot()
{
$this->loadViewComponentsAs('courier', [
Alert::class,
Button::class,
]);
}
And then use them like this:
<x-courier-alert />
<x-courier-button />
My question is if we can register those aliases without creating a package?
So what I did:
Added aliases in AppServiceProvider - default service provider for users.
<?php
namespace App\Providers;
use App\View\Components\Alert;
use App\View\Components\Button;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
$this->loadViewComponentsAs('nopackage', [
Alert::class,
Button::class,
]);
}
}
Created two components app\View\Components:
Alert.php:
<?php
namespace App\View\Components;
use Illuminate\View\Component;
class Alert extends Component
{
/**
* Create a new component instance.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Get the view / contents that represent the component.
*
* #return \Illuminate\View\View|string
*/
public function render()
{
return view('components.alert');
}
}
Button.php:
<?php
namespace App\View\Components;
use Illuminate\View\Component;
class Button extends Component
{
/**
* Create a new component instance.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Get the view / contents that represent the component.
*
* #return \Illuminate\View\View|string
*/
public function render()
{
return view('components.button');
}
}
Created two view components resources\views\components:
alert.blade.php:
<div>
<h2>Hello from Alert!</h2>
</div>
button.blade.php:
<div>
<h2>Hello from button!</h2>
</div>
Created view resources\views\bladetest.blade.php:
<h1>
Hello from blade test!
</h1>
<x-nopackage-Alert/>
Created route:
Route::view('/bladetest', 'bladetest');
And it does not work: "Unable to locate a class or view for component [nopackage-Alert]."
What I am doing wrong?

Laravel Nova - Change Tool to Resource Tool

I'm trying to convert a build Nova tool to a resource tool. I've tried to change my Tool to extend ResourceTool instead of Tool and add the resource tool to the Resource in the fields, but it's not showing up. I also changed the code of ToolServiceProvider to match that of a ResourceTool but it does not work unfortunately.
I've searched the internet but I seem to be the only one having this issue, anyone ever experienced this and know what to do? I'm not getting any error message, the resource tool just is not showing up.
Here is my code, I removed some of it for readability and confidentiality.
Product (Resource)
<?php
namespace App\Nova;
use confidentiality\SalesHistory\SalesHistory;
class Product extends Resource
{
/**
* Get the fields displayed by the resource.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function fields(Request $request)
{
return [
ID::make()->sortable(),
SalesHistory::make(),
];
}
}
SalesHistory (Resource tool)
<?php
namespace confidentiality\SalesHistory;
use Laravel\Nova\ResourceTool;
class SalesHistory extends ResourceTool
{
/**
* Get the displayable name of the resource tool.
*
* #return string
*/
public function name()
{
return 'Sales History';
}
/**
* Get the component name for the resource tool.
*
* #return string
*/
public function component()
{
return 'sales-history';
}
}
ToolServiceProvider (Resource Tool)
<?php
namespace confidentiality\SalesHistory;
use Laravel\Nova\Nova;
use Laravel\Nova\Events\ServingNova;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
class ToolServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
$this->app->booted(function () {
$this->routes();
});
Nova::serving(function (ServingNova $event) {
Nova::script('sales-history', __DIR__.'/../dist/js/tool.js');
Nova::style('sales-history', __DIR__.'/../dist/css/tool.css');
});
}
/**
* Register the tool's routes.
*
* #return void
*/
protected function routes()
{
if ($this->app->routesAreCached()) {
return;
}
Route::middleware(['nova'])
->prefix('nova-vendor/sales-history')
->group(__DIR__.'/../routes/api.php');
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
}
I managed to fix it finally. I also had to change the tool.js file to match that of a Resource Tool.
Nova.booting((Vue, router, store) => {
Vue.component('sales-history', require('./components/Tool'))
})
Try this command from the root of project: composer update

Problems creating a ComposerServiceProvider in Laravel 5.4

Im working on a Laravel 5 app and im trying to set up a ComposerServiceProvider to pass data to a couple of views (im trying now to add it to the layout/app.blade.php).
I did this following the documentation but the data im trying to add is still undefined..
In my config/app.php I added to the providers:
App\Providers\ComposerServiceProvider::class,
On ComposerServiceProvider.php
boot method:
View::composer(['layouts.app'], 'App\ViewComposers\LayoutAppComposer');
On the new created LayoutAppComposer.php
compose(View $view) method:
$metaTitle = 'MetaTitle';
$view->with('metaTitle', $metaTitle)
But When i access the url I still get:
Undefined variable: metaTitle (View: .../resources/views/layouts/app.blade.php)
Am I missing something here??
ServiceProvider
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View;
class ComposerServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
View::composer(['layouts.app'], 'App\ViewComposers\LayoutAppComposer');
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
}
}
LayoutAppComposer
<?php
namespace App\ViewComposers;
use Illuminate\Support\Facades\Session;
use Illuminate\View\View;
class LayoutAppComposer {
protected $metaTitle;
public function __construct($metaTitle)
{
$this->metaTitle = $metaTitle;
}
/**
* Bind data to the view.
*
* #param View $view
* #return void
*/
public function compose(View $view) {
$this->metaTitle = 'MetaTitle';
$view->with('metaTitle', $this->metaTitle);
}
}
Try changing:
$metaTitle = 'MetaTitle';
$view->with('metaTitle', $metaTitle)
to
$this->metaTitle = 'metaTitle';
$view->with('metaTitle', $this->metaTitle)
setup $this->metaTitle as a protected class member and assign it in the composer constructor. it may be that $metaTitle is getting garbage collected before you use it since this being resolved at the service provider level.
Since you're registering the composer with your app layout, instead you may need to use the wildcard character in place of app.layout like such:
View::composer('*', function ($view) {
//
});
To resolve $metaTile for the View Composer, try binding in your AppServiceProvider:
$this->app->bind('metaTitle', 'the string i want displayed across all views');

Composer update breaks the website Laravel 5.2

I accidentally run composer update which breaks my website. I am using Laravel 5.2. Now, I am getting this error
ErrorException in EventServiceProvider.php line 8:
Declaration of
App\Providers\
EventServiceProvider::boot(Illuminate\Contracts\Events\ Dispatcher $events) should be compatible with
Illuminate\Foundation\Support\Providers\EventServiceProvider::boot()
I tries to remove arguments from EventServiceProvider like this
/**
* Register any other events for your application.
*
* #param \Illuminate\Contracts\Events\Dispatcher $events
* #return void
*/
public function boot()
{
parent::boot();
//
}
EventServiceProvider before changes:
<?php
namespace App\Providers;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as
ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* #var array
*/
protected $listen = [
'App\Events\SomeEvent' => [
'App\Listeners\EventListener',
],
];
/**
* Register any other events for your application.
*
* #param \Illuminate\Contracts\Events\Dispatcher $events
* #return void
*/
public function boot(DispatcherContract $events)
{
parent::boot($events);
//
}
and from RouteServiceProvider.php
/**
* Define your route model bindings, pattern filters, etc.
*
* #param \Illuminate\Routing\Router $router
* #return void
*/
public function boot()
{
//
parent::boot();
}
RouteServiceProvider before changes:
<?php
namespace App\Providers;
use Illuminate\Routing\Router;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as
ServiceProvider;
class RouteServiceProvider extends ServiceProvider
{
/**
* This namespace is applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* #var string
*/
protected $namespace = 'App\Http\Controllers';
/**
* Define your route model bindings, pattern filters, etc.
*
* #param \Illuminate\Routing\Router $router
* #return void
*/
public function boot(Router $router)
{
//
parent::boot($router);
}
Now, I am getting this error:
BadMethodCallException in Macroable.php line 74:
Method controllers does not exist.
Please help me. Thank you.
Based on the comments and discussion, you've somehow ended up updating your Laravel framework to 5.3.31 which has breaking changes with 5.2. The solution would to be to either downgrade to the latest version under 5.2 or upgrade the complete application to 5.3 following the upgrade guide.
To fix with the downgrade replace the current framework package in composer.json with "laravel/framework": "5.2.*", and run composer update

Laravel 5 Command and Handler issue

I am working one of my project with laravel 5. During the implementation i got struct with one issue which is related to command and handler.
I used artisan command to generate command
php artisan make:command TestCommand --handler
I generated command at app/commands folder "TestCommand.php"
<?php
namespace App\Commands;
use App\Commands\Command;
class TestCommand extends Command
{
public $id;
public $name;
public function __construct($id, $name)
{
$this->id = $id;
$this->name = $name;
}
}
Also my TestCommandHandler.php looks like this
<?php
namespace App\Handlers\Commands;
use App\Commands\TestCommand;
use Illuminate\Queue\InteractsWithQueue;
class TestCommandHandler
{
/**
* Create the command handler.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Handle the command.
*
* #param TestCommand $command
* #return void
*/
public function handle(TestCommand $command)
{
dd($command);
}
}
Whenever dispatch this command from controller it shows following issue
InvalidArgumentException in Dispatcher.php line 335:
No handler registered for command [App\Commands\TestCommand]
Please, Anybody help me to solve this problem. Thank you
By default Laravel 5.1.x does not included BusServiceProvider. So we should create BusServiceProvider.php under provider folder and include that in to config/app.php.
BusServiceProvider.php
<?php namespace App\Providers;
use Illuminate\Bus\Dispatcher;
use Illuminate\Support\ServiceProvider;
class BusServiceProvider extends ServiceProvider {
/**
* Bootstrap any application services.
*
* #param \Illuminate\Bus\Dispatcher $dispatcher
* #return void
*/
public function boot(Dispatcher $dispatcher)
{
$dispatcher->mapUsing(function($command)
{
return Dispatcher::simpleMapping(
$command, 'App\Commands', 'App\Handlers\Commands'
);
});
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
}
config/app.php
'providers' => [
App\Providers\BusServiceProvider::class
]
So it may help others. Thank you

Resources