based on my domain names i need to separate the view folder,so i have created ModifyViewFolder class file in middleware and also registered in kernel.php but its not working properly dont know how to check working or not.and also please verify my kernel.php file in this i dont know registered right or not.
file path: 'View'=> \App\Http\Middleware\ModifyViewFolder::class
use Closure;
use Illuminate\View\FileViewFinder;
use Illuminate\Support\Facades\View;
class ModifyViewFolder
{
public function handle($request, Closure $next)
{
$finder = new FileViewFinder(app()['files'], [
app_path('../resources/views/' . $request->server->get('HTTP_HOST')),
app_path('../resources/views/'),
]);
View::setFinder($finder);
return $next($request);
}
}
kernel.php: App\Http\kernel.php
protected $routeMiddleware = [
'View'=> \App\Http\Middleware\ModifyViewFolder::class,
];
Please try this below . Please assign the path in $middleware.
protected $middleware = [
\App\Http\Middleware\ModifyViewFolder::class,
];
Related
I create an Event Controller to log all the request to my APIs. I know that using a controller inside other controller is not a good idea, so... Where do I have to implement it?
EventController:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Event;
class EventController extends Controller
{
protected static $instance = null;
/** call this method to get instance */
public static function instance(){
if (static::$instance === null){
static::$instance = new static();
}
return static::$instance;
}
/** protected to prevent cloning */
protected function __clone(){
}
/** protected to prevent instantiation from outside of the class */
protected function __construct(){
}
public function create($type, $description){
Event::create([
'id_type' => $type,
'date_time' => date('Y-m-d H:i:s'),
'id_users' => auth()->user()->id,
'description' => $description
]);
}
}
2 way i suggest:
1.make an event and fire it in all actions.
2.make a middleware and add it in your each routing(or add in routegroup)
Second one is better.because middlewares made exactly for this reason.All requests that sends to server should pass middleware first .
In brief:you should create middleware with php artisan make:middleware yourMiddlewareName and after add your controller code in it you should add name of this middleware in kernel.php in middlewares array.
Now its ready for assign it for every routes you want by append ->middleware("yourMiddlewareName") in end if each one.
I want to make the access to /Inscription in my website unavalible untill the admin gives access to it in the /admin page soo when the guest goes to /inscription he gets a message "unavalible" untill the admin goes to /admin and unlock it
i tried to make it using the middleware on laravel but i doesn't seem the work .
i did php artisan make:middleware Access
and coded and made a view unavalible i want it to load when he goes to /Inscription
kernel.php :
protected $middleware = [
.....
\App\Http\Middleware\Access::class,
];
protected $routeMiddleware = [
'access' => \App\Http\Middleware\Access::class,
the access middleware :
<?php
namespace App\Http\Middleware;
use Closure;
class Access
{
public function handle($request, Closure $next)
{
echo "mwajer";
return $next($request);
}
}
Route::group(['middleware' =>['access']], function (){
Route::get('/inscription', MyInscriptionController#index)->name('inscription');
//All your routes that you needs admin approval here
});
Put all your restricted route inside the group, also it is a good idea to use route naming. You can add multiple middleware to the group
I'm working on a Laravel based project and I need to execute some "basic" php code on every page load. Until now, I placed my code in boot() from AppServiceProvider. It works well, but I need to execute my "basic" code only after the code from route's controller has been already executed.
I've already searched in laravel's official docs, but I still did not figured out how to do it.
This is how my route looks:
Route::match(['get', 'post'], '/profile/email/{profile_id?}', 'profileController#profileHandleEmail')->name('profile/email');
The result I want to achive is to execute the code from profileController#profileHandleEmail before the "basic" code from AppServiceProvider.
Which would be the best way to do this? I guess it can't be achived using AppServiceProvider.
The suggested way to achieve what you want is to use middleware:
Run php artisan make:middleware PostProcess
It should generate the a middleware class under App\Http\Middleware
class PostProcess {
public function handle($request, $next) {
$response = $next($request);
// Run you code here.
return $response
}
}
Then modify your App\Http\Kernel.php middleware:
protected $middleware = [
//Existing entries
\App\Http\Middleware\PostProcess::class
];
That middleware will run after the response has been generated but before the response is sent to the client. If you want to run the code after the response was sent to the client you can use terminable middleware
class PostProcess {
public function handle($request, $next) {
return $next($request);
}
public function terminate($request, $response) {
//Your code here
}
}
Step1 : Create Middleware
php artisan make:middleware PostProcess
Step2 : Make a code which you need
class PostProcess {
public function handle($request, $next) {
if(condition){
you code here
}
return $next($request);
}
}
Step3 : Call middleware in kernel.php
protected $routeMiddleware = [
'admin' => \App\Http\Middleware\PostProcess::class,
];
Step 4: Call middleware in route file
Route::group(['middleware' => 'admin'], function() {
});
Im am new in Laravel and Im tryig to grab in the route, everything that comes after locale ("us_en/").
This my controler
<?php
namespace App\Http\Controllers\SitePublic;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\App;
class HomeController extends Controller
{
public function index(Request $request,$account,$location) {
dd($request->path());
$loc=explode("_", $location);
$locale = $loc[0];
$lang= $loc[1];
App::setLocale($lang);
return view('homeProfPublic')->with(['account' => $account,'active' => 'home','lang'=>$lang,"locale"=>$locale]);
}
}
For now I am using $request->path() to grab everything in the route.
My Route map is like this
mySite.com/userAccount =>User's home page with English as default language
mySite.com/userAccount/us_sp =>User's home page in Spanish
mySite.com/userAccount/us_sp/contact =>User's contac page in Spanish
...
So the $request->path() could give me the full URL and I could use php explode to grab all after locale (if it is the case).
So my question is if is there some function or method already done in Laravel to solve this situation? If yes what is it?
You don't need to use $request
If you set the route properly: you can receive the parameters in your controller function
You can set either 3 routes to separate the logic in different functions or one with optional parameters
Route::get('/userAccount/{language?}/{method?}', 'YourController#index');
And then, in your controller specify default values for the parameters:
public function index($language=null, $method=null) {
list($locale, $lang) = explode('_', $language);
var_dump($locale);
var_dump($lang);
var_dump($method);
}
And your will get for the url /userAccount/us_sp/contact
string 'us' (length=2)
string 'sp' (length=2)
string 'contact' (length=7)
UPDATE You can create a middleware and process all the urls from there
php artisan make:middleware ProcessLanguageRoutes
You need to register the middleware in the app/Http/Kernel.php file
protected $middlewareGroups = [
'web' => [
...
\App\Http\Middleware\ProcessLanguageMenu::class,
],
And then, you can process the data in the handle function checking the $request variable
public function handle($request, Closure $next)
{
if ($request) { // check something
// do something
}
return $next($request);
}
Is there a way to turn off some Laravel 5.0 middleware for functional tests?
Just edit app/Http/kernel.php file and comment any undesired middleware line in array $middleware.
You don't need to comment the ones in $routeMiddleware since these won't be automatically called, and they need to be specifically activated in the routes.php file.
Another way:
Copy Kernel.php as Kerneltest.php in the same folder.
Then rename the class in Kerneltest.php to Kerneltestand make it extends Kernel.
Then remove any middleware lines from Kerneltest
Then add the following to bootstrap\app.php :
$app->singleton(
'Illuminate\Contracts\Http\Kerneltest',
'App\Http\Kerneltest'
);
Then in public\index.php use
$kernel = $app->make('Illuminate\Contracts\Http\Kerneltest');
or
$kernel = $app->make('Illuminate\Contracts\Http\Kernel');
depending on whether you're testing or not.
I've found simple solution, although probably it may be not "TRUE", but it works. I've modified method handle() in my app/Http/Middleware/VerifyCsrfToken.php to this
public function handle($request, Closure $next)
{
if (env('APP_ENV') === 'testing') {
return $next($request);
}
return parent::handle($request, $next);
}