lundi 2 novembre 2015

Blade passing values from view to master and then pass it to header.blade

I have a situation where i have a view file users.blade.php which extends master. and the master includes another file called header.blade.php, i want to pass some data from users.blade to header.blade.

Here is the simplified version of the files

/****** Users.blade.php *********/
@extends('shared.master')
@section('title', 'Dashboard')

@section('pagecss')
    <link rel="stylesheet" href="links to css file" />
@endsection

Here is the master.blade.php

/******Shared/master.blade.php ********/
<html>
    <head>
        <title>@yield('title')</title>
        @include('shared.header')
    </head>
    <body >

        @yield('content')

    </body>
</html>

Here is the header file

/******Shared/header.blade.php ********/
<link rel="stylesheet" href="links to bootstrap" />
@yield('pagecss')
<link rel="stylesheet" href="links to other files" />

@yield('pagecss') doesn't work in the header.blade, but it does work in master.blade. Now i cant paste it in master.blade because there are some files which override other files, so it has to be in a specific order. Any ideas how to make the yield work in header.blade?



via Chebli Mohamed

Laravel 5 pagination send to veiw with other variables

I have this bunch of codes

        $result= AccessControl::getUserAccess("/admin/users/user");
        if($result==1)
         {
             return redirect("/");
         }
         else
         {
           //$user= Users::Tenant((int)session()->get('tenantId'))->get();
           $user= Users::Tenant((int)session()->get('tenantId'))->paginate();
           return response()->json(["Users"=>$user,"Access_level"=>$result]);
         } 

With the above, I kept getting an null for $user object on the front-end. But when I returned the$user object, that contains all expected for the pagination. How do I include Access_level as part of that to the user using json still?



via Chebli Mohamed

Laravel 5.1 - Setting X-Frame-Options Causes Error

I added the following middleware to the HTTP Kernel stack, in app/Http/Kernal.php, which adds the X-Frame-Options: SAMEORIGIN to the header.

<?php

namespace App\Http\Middleware;

use Closure;

class FrameGuard
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $request->headers->set('X-Frame-Options', 'SAMEORIGIN');

        return $next($request);
    }
}

It causes a none-related error to show and part of the view is also rendered. This part of the view is rendering a list from the database, which works when the FrameGuard middleware is removed from the stack.

FatalErrorException in
MySqlGrammar.php line 139:
Maximum function nesting level of '100' reached, aborting!

enter image description here

This is the middleware stack in app/Http/Kernel.php.

protected $middleware = [
    CheckForMaintenanceMode::class,
    Middleware\EncryptCookies::class,
    AddQueuedCookiesToResponse::class,
    StartSession::class,
    ShareErrorsFromSession::class,
    Middleware\AccessControlAllowOrigin::class,
    FrameGuard::class,
];

When FrameGuard::class is removed, everything works as expected, however when FrameGuard::class is added it causes the above error. Further more, when I keep FrameGuard::class in the stack and comment out $request->headers->set('X-Frame-Options', 'SAMEORIGIN'); inside of the FrameGuard middleware class, I get the same error.

Has anyone else ran into something similar or am I going about adding this header in an incorrect way?



via Chebli Mohamed

Preview post functionality (like in Wordpress) in a Laravel's blog

I'd like to add to my Laravel's blog app the ability to open a preview of a post in a new window/tab before saving it to the DB (like in Wordpress). Which is the best way to do it?



via Chebli Mohamed

Laravel5: NotFoundHttpException in RouteCollection.php line 161

I've clone a repository from github,but getting error in routing for admin, the same code is running well on another developer machine, we both are using Window 8.

We are using Auth Middleware to validate user for logged in but when I tried to access the admin http://ift.tt/1691lrz I am getting this error

NotFoundHttpException in RouteCollection.php line 161

But frontend works fine. I've tried to execute following command in terminal but same issue after that

composer install
php artisan clear--compiled

Also I checked the registered routs & its showing in terminal by using command

php artisan route:list

enter image description here

Here are my route.php

Route::get('/', function () {
    return view('welcome');
});


// Logging in and out
get('/login', 'Auth\AuthController@getLogin');
post('/login', 'Auth\AuthController@postLogin');
get('/logout', 'Auth\AuthController@getLogout');

Route::group(['namespace' => 'Admin','prefix' => 'admin','middleware' => 'auth',], function () {

    resource('/', 'AdminController');

    resource('/countries', 'CountryController');

});

.htaccess file code

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>



via Chebli Mohamed

Record video and store it in to storage folder in Laravel

In my Laravel project I have to integrate a module that is, record live video and save it in to storage folder in Laravel. So I have downloaded a library RecordRTC-to-PHP

I have tested this in simple PHP. It works fine. But I stuck to include this in my Laravel project. Please check below code and help me to convert simple PHP code to Laravel5 code.

JS

var fileType = 'video'; // or "audio"
var fileName = 'ABCDEF.webm';  // or "wav"

var formData = new FormData();
formData.append(fileType + '-filename', fileName);
formData.append(fileType + '-blob', blob);

xhr('save.php', formData, function (fName) {
    window.open(location.href + fName);
});

function xhr(url, data, callback) {
    var request = new XMLHttpRequest();
    request.onreadystatechange = function () {
        if (request.readyState == 4 && request.status == 200) {
            callback(location.href + request.responseText);
        }
    };
    request.open('POST', url);
    request.send(data);
}

PHP

<?php
foreach(array('video', 'audio') as $type) {
    if (isset($_FILES["${type}-blob"])) {

        $fileName = $_POST["${type}-filename"];
        $uploadDirectory = DIR.'/uploads/'.$fileName;

        if (!move_uploaded_file($_FILES["${type}-blob"]["tmp_name"], $uploadDirectory)) {
            echo(" problem moving uploaded file");
        }

        echo($uploadDirectory);
    }
}
?>

Laravel Controller

public function store(RecordingvideoRequest $request)
    {
        $file   = $request->file("video-blob");
        dd($file);
    }

In controller result is showing null



via Chebli Mohamed

How to use Codeception to test laravel 5 APIs?

I am new to Codeception and I am trying to test my web service which I have built using Laravel 5. I am following the guide here.

So I created a suite called api first:

codecept generate:suite api

api.suite.yml

class_name: ApiTester
modules:
    enabled:
        - REST:
            url: http://localhost:8000/api/
            depends: Laravel5

AuthenticateUserCept.php

<?php 
$I = new ApiTester($scenario);
$I->wantTo('authenticate a user');
$I->haveHttpHeader('Content-Type', 'application/x-www-form-urlencoded');
$I->sendPOST('/authenticate', [
    'username' => 'archive',
    'email' => 'admin@admin.com',
    'password' => 'password'
]);
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();

I have already tried this using Postman and it runs fine. The /authenticate route takes the 3 parameters and spits out a JWT token happily but I cannot make it work with Codeception.

1) Failed to authenticate a user in AuthenticateUserCept (tests/api//AuthenticateUserCept.php)

 Step  I see response code is 200
 Fail  Failed asserting that 500 matches expected 200.

Scenario Steps:

 3. $I->seeResponseCodeIs(200)
 2. $I->sendPOST("/authenticate",{"username":"archive","email":"admin@admin.com","password":"password"})
 1. $I->haveHttpHeader("Content-Type","application/x-www-form-urlencoded")

Where am I going wrong? And moreover it is still blurry to me that how do I refactor this particular test in order to have a JWT for other requests that I make. Any help is appreciated.



via Chebli Mohamed