Not able to access module directory - php

i generated one module which is for web services and module name is "service".
i added code in web.php
'modules'=>[
'service' => [
'class' => 'app\modules\service\service',
],
],
but i am not able to access this module in browser with below URL
http://localhost/projectfoldername/service/login/index
service = Module [ dir : protected/modules/service/service.php]
login = Controller [ dir : protected/modules/service/controllers/loginController.php]
index = action
below is loginController.php code
<?php
namespace app\modules\service\controllers;
class loginController extends \yii\web\Controller
{
public function actionIndex()
{
echo "test";
exit();
// return $this->render('index');
}
}
please help me if i am missing anything.

Controllers directory where your loginController is, should be within module's directory.
protected/modules/service/controllers/loginController.php

Related

Codeigniter 4 - Cannot get my module to work

I am trying to get a simple module working. All it does is echo 'test' to a web page but I cannot get it working.
The first thing I did was add the module to app/Config/Autoload.php as follows;
public $psr4 = [
APP_NAMESPACE => APPPATH, // For custom app namespace
'Config' => APPPATH . 'Config',
'Modules\Filemanager' => ROOTPATH . 'modules'
];
Then, I created the following directory structure;
In Modules/Filemanager/Config/Routes.php I have added following route;
<?php
$routes->add('/filemanager/(:any)', 'Modules\Filemanager\Controllers\Filemanager::index');
Lastly, in Modules/Filemanager/Controllers/Filemanager I have the following method:
<?php
namespace Modules\Filemanager\Controllers;
use App\Controllers\BaseController;
class Filemanager extends \App\Controllers\BaseController
{
public function index(){
echo 'test'; die();
}
}
When I go to my browser and enter example.com/filemanager/index I get the following error;
Controller or its method is not found: \App\Controllers\Filemanager::index
Any ideas would be much appreciated.
You've done few things wrong here.
In the App/Config/Autoload.php,
you've written 'Modules\Filemanager' => ROOTPATH . 'modules'
But in your directory structure image, there is no directory with the name 'modules'
Please change the 'Modules' folder name to 'modules'
Change 'Modules\Filemanager' => ROOTPATH . 'modules' to
'Modules' => ROOTPATH . 'modules' in App/Config/Autoload.php
In the Modules/Filemanager/Config/Routes.php
You're using the $routes variable in the below line
$routes->add('/filemanager/(:any)', 'Modules\Filemanager\Controllers\Filemanager::index'); But $routes is not defined anywhere in the file.
Put
if(!isset($routes)) {
$routes = App\Config\Services::routes(true);
}
$routes->get('/filemanager/(:any)', 'Modules\Filemanager\Controllers\Filemanager::index');
It should be $routes->get() instead of $routes->add()
Module level routing will not work until you add these routes in the App\Config\Routes.php file.
To add your modules routes to the app\Config\Routes.php
foreach(glob(ROOTPATH . 'modules/*', GLOB_ONLYDIR) as $item_dir) {
if(file_exists($item_dir . '/Config/Routes.php')) {
require_once($item_dir . '/Config/Routes.php');
}
}
After these changes your code should work.

Laravel: access view composer's data in other file(s)

I have a view composer file called statistics.blade.php which is accessed on every page in the application(also included on the dashboard). On the dashboard page the same set of data is displayed in the form tiles.
class StatisticsComposer
{
public function compose(View $view)
{
# models
$ModelA = new ModelA();
$ModelB = new ModelB();
$ModelC = new ModelC();
...
# binding data
$view->with('arrayStatistics', [
'ModelA' => $ModelA->_someMethod(),
'ModelB' => $ModelB->_someMethod(),
'ModelC' => $ModelC->_someMethod(),
...
]);
}
}
I need to access this arrayStatistics array on the dashboard index file. Is it possible..?
After you created your StatisticsComposer then you need to boot it in service provider.
Create a service provider called ComposerServiceProvider such as ;
class ComposerServiceProvider extends ServiceProvider
{
public function boot()
{
View::composer(['statistics'], StatisticsComposer::class); // assuming it is in `resources` folder
// other view composer bindings...
}
}
and at it to app.php's providers array such as;
'providers' => [
// ... other providers
App\Providers\ComposerServiceProvider::class,
],
Then $arrayStatistics will be accessible on your statistics.blade.
Edit:
I think it is better to not use arrayStatistics but ModelA, ModelB directly for direct usage/access.

Laravel Socialite InvalidStateException in AbstractProvider.php when reloading my page after facebook login

My expected result is to have the user redirected to my homepage after facebook login. I am using Socialite, Laravel 5.4, and Xampp. After being able to login through facebook, my url is now in callback in which the callback is calling the logincontroller that redirects to the homepage. So problem is after redirected to homepage, my url has now some hashed value. localhost/sampleProject/public/login/facebook/callback?code=AQBZpjdW... and when I reload page it shows error invalid state exception in abstractprovider.php. Am I missing something in my functions?
Inside my login controller
public function redirectToProvider()
{
return Socialite::driver('facebook')->redirect();
}
/**
* Obtain the user information from facebook.
*
* #return Response
*/
public function handleProviderCallback()
{
$user = Socialite::driver('facebook')->user();
return view('user-profile', compact('user',$user));
}
Inside my services.php file
'facebook' => [
'client_id' => 'insert_app_id_here',
'client_secret' => 'enter_app_secret_here',
'redirect' => 'http://localhost/sampleProject/public/login/facebook/callback',
],
Inside my routes/web.php
Route::get('login/facebook', 'Auth\LoginController#redirectToProvider');
Route::get('login/facebook/callback', 'Auth\LoginController#handleProviderCallback');
I also have included these in my config/app.php
Laravel\Socialite\SocialiteServiceProvider::class,
and
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
You could try this thing)
public function handleProviderCallback(Request $request)
{
session()->put('state', $request->input('state'));
$user = Socialite::driver('facebook')->user();
return view('user-profile', compact('user',$user));
}
First of all, run the following command:
composer require laravel/socialite
After that In app/config.php add the following line in providers.
Laravel\Socialite\SocialiteServiceProvider::class,
After that In app/config.php add the following line in aliases
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
In config/services.php add:
//Socialite
'facebook' => [
'client_id' => '1234567890444',
'client_secret' => '1aa2af333336fffvvvffffvff',
'redirect' => 'http://laravel.dev/login/callback/facebook',
],
Now create two routes, mine are like these:
//Social Login
Route::get('/login/{provider?}',[
'uses' => 'AuthController#getSocialAuth',
'as' => 'auth.getSocialAuth'
]);
Route::get('/login/callback/{provider?}',[
'uses' => 'AuthController#getSocialAuthCallback',
'as' => 'auth.getSocialAuthCallback'
]);
You also need to create controller for the routes above like so:
<?php namespace App\Http\Controllers;
use Laravel\Socialite\Contracts\Factory as Socialite;
class AuthController extends Controller
{
public function __construct(Socialite $socialite){
$this->socialite = $socialite;
}
public function getSocialAuth($provider=null)
{
if(!config("services.$provider")) abort('404'); //just to handle providers that doesn't exist
return $this->socialite->with($provider)->redirect();
}
public function getSocialAuthCallback($provider=null)
{
if($user = $this->socialite->with($provider)->user()){
dd($user);
}else{
return 'something went wrong';
}
}
}
and finally, add Site URL to your Facebook App. I followed this article for the proposed solution above. https://www.cloudways.com/blog/social-login-in-laravel-using-socialite/

How to create a Enom component in Yii framework

I am new to yii and i have to create a yii component for Enom api .I have followed this url Enom application for refrence . It is in core php and i want to implement this in yii as component or module .I have done in this way
put the files interface and class in the yii component folder.
modify the class as mentioned here yii custom component . Now my class name is EnomService and interface name is EnomInterface
i have added these lines also in my class
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
modified the main.php file in config folder:
'import'=>array(
'application.models.*',
'application.components.*',
),
'defaultController'=>'post',
// application components
'components'=>array(
'user'=>array(
// enable cookie-based authentication
'allowAutoLogin'=>true,
),
'mycomponent' => [
'class' => 'app\components\EnomService',
],
calling in the controller in this way .
public function actionEnom ()
{
echo "asdgsgsag";
$enom = new EnomService('manoj_rudra', 'manoj#41#', false, true);
$enom->debug = true;
$result= Yii::$app->EnomService->checkDomain('systurn', 'com', true);
//$result = $enom->checkDomain('systurn', 'com', true); // This enables domain spinner
echo '<pre>';
var_dump($result);
echo '</pre>';
}
But it is not working . I am not so much familiar with yii custom component . Please help me to create this .
Are you using Yii or Yii2?
If it is Yii, then you could use plenty of other existing extensions to inspire yourself, for example this one: https://github.com/HeavyDots/yii-sms
As for Yii2 you could do something similar, look into already existing extensions for Yii2 on YiiFramework website and you can see how component classes are defined.
I would recommend:
1) Create a new directory inside "components" named "enom"
2) Place inside that directory all your enom files from https://github.com/comdexxsolutionsllc/MoondayFramework/tree/master/engine/enom
3) Create the component class called "Enom.php" inside the directory, something like this:
<?php
// include enom service class
require(dirname(__FILE__).'/class.EnomService.php');
namespace components\enom;
use Yii;
class Enom extends \yii\base\Component
{
// define private property to store service
private $service;
public function init()
{
parent::init();
// init the service
$this->service=new EnomService('manoj_rudra', 'manoj#41#', false, true);
}
/**
* #return EnomService
*/
public function getService() {
return $this->service;
}
}
?>
4) Then in the configuration properly define the component
'enom' => [
'class' => 'app\components\enom\Enom',
],
5) And finally use it like this
Yii::$app->enom->getService()->checkDomain
As I said before, haven't used Yii2 yet so this might need tweaking but could point you on the right path.

Laravel Trying to Call Model Class Returns Not Found

This is in my routes.php :
Route::post('/', function()
{
$rules = array(
'email' => 'required|email'
);
$validation = Validator::make(Input::all(), $rules);
if ($validation->fails())
{
echo "Not a valid e-mail";
}
else
{
$subscriber = Subscriber::where('email','=', Input::get('email') );
if ($subscriber)
{
echo "Email already exists";
}
else
{
Subscriber::create(array(
'email' => Input::get('email')
));
In my models folder, I have the following class defined:
<?php
class Subscriber extends Eloquent {
protected $table = 'subscribers';
protected $fillable = array('email');
public $timestamps = false;
}
?>
When I try and insert information into the database, it's telling me that Class 'Subscriber' is not found. Am I missing a connection of some sort?
Make sure you run composer dumpautoload when you create your model, so Laravel knows the new class exists.
Class files are automatically pulled in based on your autoload.php file. This file is dynamically generated via composer. You only have to run this once each time you create a new class file. If you modify an existing one, it's not needed.
Try composer dump-autoload and php artisan dump-autoload. It will regenerate all class files and class loader
Sorry to resurrect an old thread. You need to make sure that the file imports the class. Do this by adding the following line to the top of your routes.php file:
use App\Subscriber;
Note: This assumes that your generated class is in the App folder, which is the default.

Categories