lundi 2 novembre 2015

Laravel 5.1 - Method [middlewere] does not exits

I am trying to add a middleware, but I am getting a strange problem.

What I have done is-

Create a middleware like it-

Middleware/APIMiddleware.php

<?php

namespace App\Http\Middleware;

use Closure;

class APIMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        var_dump($request);

        return $next($request);
    }
}

Then register it in kernel.php

protected $routeMiddleware = [
    'auth'          => \App\Http\Middleware\Authenticate::class,
    'auth.basic'    => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'guest'         => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'api'           => \App\Http\Middleware\APIMiddleware::class,
];

Now use the 'api' middleware-

<?php
namespace App\Http\Controllers\API\Customer;

//use App\Http\Controllers\Controller;
use App\Http\Controllers\API\APIAuth;

class ListShowingController extends APIAuth
{
    public function __construct()
    {
        $this->middlewere('api');
    }

    public function dummy()
    {
        //return response()->view('welcome');
        $token_length=150;

        return response()->json([
                                    'name' => 'Abigail',
                                    'state' => 'CA',
                                    'states' => array(
                                                        'a' => "SSSSSS",
                                                        'b' => 8
                                                    ),
                                    'access_token' => bin2hex(random_bytes($token_length))
                                ]);
    }
}

And the routes.php is-

<?php

//Routes for mobile API
Route::group([ 'prefix' => 'api/customer', 'namespace' => 'API\Customer'],     function()
{
    //prefix    is for api/customer/{given_url}
    //namespace is for creating folder in controllers folder (API\Customer)
    Route::get('/',
    [
        'as' => 'API.index',
        'uses' => 'ListShowingController@dummy'
    ]);
});

File structure is like this-

enter image description here

I am getting this error-

enter image description here

Can anyone help me please?



via Chebli Mohamed

Laravel 5: Pagination links on raw query does not work

I have a raw query which is working properly but i am trying to paginate it as below

$query_result = DB::select($query);
$query_result = new Paginator($query_result, 5, 1);
return view('pages.printout', compact('query_result'));

And in blade

{!! str_replace('/?', '?', $query_result->render()) !!}

First page comes up okay, it paginates first 5 records but when i click to pagination links no change happens. Any help would be appreciated.



via Chebli Mohamed

Laravel 5 Cookie:get returns null if EncryptCookies turned on

I'm trying to store some information in cookies in this way:

setcookie("o_favourites", serialize($fav_ids), time()+60*60*24*1825);

If EncryptCookies is turned on (by adding \App\Http\Middleware\EncryptCookies::class, presents in $middleware section of app/Http/Kernel.php) I recieved null when trying to get cookies:

$cookie = Cookie::get('o_favourites');

What could cause this behaviour?


BTW I use setcookies(), because Cookie::queue(Cookie::forever('o_favourites', serialize($fav_ids), null, null, false, false)); seems not working



via Chebli Mohamed

After moving Laravel 5 project from XAMPP to Ubuntu server the views cannot be found

Working on XAMPP everything seems to work fine, but now that I moved the project to our internal Ubuntu server I am getting errors that views cannot be found. While developing I moved the views directory to app/Views (instead of public/views) and changed this in config/view.php as below:

'paths' => [
    realpath(app_path('Views'))
]

And the error is:

[2015-11-02 16:33:42] local.ERROR: exception 'InvalidArgumentException' with message 'View [base\frontend\baywest2015\pages\page] not found.' in /var/www/baywest2015/vendor/laravel/framework/src/Illuminate/View/FileViewFinder.php:140
Stack trace:
#0 /var/www/baywest2015/vendor/laravel/framework/src/Illuminate/View/FileViewFinder.php(77): Illuminate\View\FileViewFinder->findInPaths('base\\frontend\\b...', Array)
#1 /var/www/baywest2015/vendor/laravel/framework/src/Illuminate/View/Factory.php(145): Illuminate\View\FileViewFinder->find('base\\frontend\\b...')
#2 /var/www/baywest2015/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(592): Illuminate\View\Factory->make('base\\frontend\\b...', Array, Array)
#3 /var/www/baywest2015/app/Http/Controllers/Frontend/PagesController.php(63): view('base\\frontend\\b...', Array)
#4 [internal function]: App\Http\Controllers\Frontend\PagesController->page()

So at first I thought this is a typical capital letter issue, but everywhere I look, the Views is set with a capital V.

Am I missing something?

By the way, moving from one XAMPP environment to another works great



via Chebli Mohamed

error Trying to get property of non-object in Laravel 5 with relation and scope

I'm working in Laravel 5. My situation is the next: I have a table USERS and a table PROFILES the have a relation 1:1. So I have in the model users a method called profile like this:

public function profile(){      
        return $this->belongsTo('App\models\Profile','profile_id');
    }

Also I have a method scopeData where I want to filter what users have identity_doc equal as data:

public function scopeData($query,$data){
        $profile=$this->profile;
        $query->where($profile->identity_doc,$data);
    }

in the user controller I use the scope function in this line:

$students= Users::data($request->get('data'))->where('rol','=','Student')->get();

but, when I runned it I have this error: Trying to get property of non-object so, why it happens? the profile method works fine in others functions, but no here, I get:

**array('query' => object(Builder), 'data' => 'example', 'profile' => null))**



via Chebli Mohamed

translation not interpreted in Laravel 5

In laravel 5, when I write this in my view:

@include("tournaments.form", ["submitButton" => "  {{trans('crud.addTournament')}}" ])

I get this:

<?php echo e(trans('crud.addTournament')); ?>

How can I do so that it can be interpreted?



via Chebli Mohamed

Bootstrap CSS form class is overiding btn class for submit button styling

I'm using laravel to create a form styled with bootstrap css.

{!! Form::open(['url' => '/hold', 'method' => 'POST', 'class' => 'form-horizontal']) !!}

    <div class="form-group">
        {!! Form::label($detail->name, NULL, array('class'=>'control-label')) !!}
        {!! Form::text('detail['.$detail->name.']', null, ['class' => 'controls']) !!}
    </div>

    <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
    {!! Form::submit('Hold', ['class'=>'btn btn-danger', 'style' => 'float:right;']) !!}

{!! Form::close() !!}

creates a disparity in button sizes

According to Chrome dev tools, the css selector that is overriding the .btn style rules is button, html input[type="button"], input[type="reset"], input[type="submit"]

I'm guessing that the html input class takes precedence over the btn class. I do not know how to prevent this and force btn to take precedence.



via Chebli Mohamed