Laravel Backpack Admin CRUD Views returning 404 - laravel-backpack

I had this working on a local dev environment, but now that I'm pushing it live I'm running into an error:
When I try to access my CRUD pages (/admin/images or similar), I get taken to my websites 404 page.
I uploaded the /routes/admin.php file, all my resource files, controllers, models, vendor files, public/vendor files, and probably some others I'm forgetting to mention.
Not sure if theres something in the config files for backpack I need to edit or what. Looking for some direction.
Note: I am able to access the default routes from Backpack (dashboard, login, logout)
RouteServiceProvider.php
protected function mapWebRoutes()
{
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
}
protected function mapApiRoutes()
{
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
protected function mapAdminRoutes()
{
Route::middleware(['web', 'admin'])
->prefix('admin') // or use the prefix from CRUD config
->namespace($this->namespace.'\Admin')
->group(base_path('routes/admin.php'));
}
Found this error in the error logs:
exception 'Illuminate\Database\Eloquent\RelationNotFoundException'
with message 'Call to undefined relationship [wheels] on model
[App\Models\WheelFinishes].' in
laravel/framework/src/Illuminate/Database/Eloquent/RelationNotFoundException.php:20
But I have the relationship defined in my WheelFinishes model
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\CrudTrait;
class WheelFinishes extends Model
{
use CrudTrait;
public function wheels()
{
return $this->belongsTo('App\Models\Wheels', 'wheel_id');
}
...
}
Wheels Model
namespace App\Models;
use Laravel\Scout\Searchable;
use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\CrudTrait;
use App\User;
class Wheels extends Model
{
use CrudTrait;
protected $table = "wheels";
protected $primaryKey = 'id';
...
public function tips()
{
return $this->hasMany('App\Models\WheelTips', 'wheel_id');
}
public function finishes()
{
return $this->hasMany('App\Models\WheelFinishes', 'wheel_id')->where('status', '=', '1')->orderBy('order');
}
public function factoryFinishes()
{
return $this->hasMany('App\Models\WheelFinishes', 'wheel_id')->where('status', '=', '1')->where('factory_finish', '=', '1')->orderBy('order');
}
public function wheelImages()
{
return $this->hasMany('App\Models\WheelImages', 'wheel_id');
}
public function wheelImage()
{
return $this->hasOne('App\Models\WheelFinishes', 'wheel_id')->where('status', '=', '1')->orderBy('order');
}
public function profile()
{
return $this->BelongsTo('App\Models\Profile');
}
public function series()
{
return $this->BelongsTo('App\Models\Series');
}
public function vehicles()
{
return $this->hasMany('App\Models\Vehicles', 'wheel_id')->where('status', '=', '1')->orderBy('order')->take(3);
}
public function vehicle()
{
return $this->hasOne('App\Models\Vehicles', 'wheel_id')->where('status', '=', '1')->orderBy('order');
}
}
routes.php
<?php
// Backpack\CRUD: Define the resources for the entities you want to CRUD.
CRUD::resource('video', 'VideoCrudController');
CRUD::resource('wheels', 'WheelCrudController');
Route::get('finishes/ajax-finishes-options', 'FinishCrudController#wheelsOptions');
CRUD::resource('finishes', 'FinishCrudController');
Route::get('albums/ajax-albums-options', 'AlbumCrudController#albumsOptions');
CRUD::resource('albums', 'AlbumCrudController');
CRUD::resource('heros', 'HeroCrudController');

If you have a separate route file you probably need to register it in RouteServiceProvider:
Route::group([
'middleware' => 'web',
'namespace' => $this->namespace,
], function ($router) {
require base_path('routes/web.php');
require base_path('routes/admin.php');
});

Related

Best practice to redirect Laravel home to another URL?

How I can redirect the Laravel 7 auth home URL to the dashboard.
My route filE route/web.php
use Illuminate\Support\Facades\Route;
Auth::routes();
Route::get('/dashboard', 'HomeController#index')->name('dashboard');
Route::get('/', function () {
return view('/home');
})->middleware('auth');
MyLogin Controller seems like this
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = RouteServiceProvider::HOME;
public function __construct()
{
$this->middleware('guest')->except('logout');
}
}
After spending a few hours on this I found the below solutions
Just Make Changes in app\Providers\RouteServiceProvider.php
public const HOME = '/home';
To
public const DASHBOARD = '/dashboard';
Then make small changes in another files i.e app\Http\Controllers\Auth\LoginController.php
protected $redirectTo = RouteServiceProvider::HOME;
To
protected $redirectTo = RouteServiceProvider::DASHBOARD;
Make a final change in app\Http\Middleware\RedirectIfAuthenticated.php
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
return redirect(RouteServiceProvider::HOME);
}
return $next($request);
}
To
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
return redirect(RouteServiceProvider::PARTNERS);
}
return $next($request);
}
Change your rout to
Route::get('/', function () {
return redirect('/dashboard');
})->middleware('auth');
Not open the CMD and inside the project folder and run the command php artisan optmize:clear and check now by the login.
So simple solution is :
use Illuminate\Support\Facades\Route;
Auth::routes();
Route::get('/dashboard', 'HomeController#index')->name('dashboard');
Route::get('/', 'HomeController#index');

Kohana rest api implementaion: how to start with SupersonicAds/kohana-restful-api?

I am new to kohana framework. I need to implement rest api for my application.
I have downloded rest api from https://github.com/SupersonicAds/kohana-restful-api and placed in my localhost. Under modules. now the file structre is
I have enabled module in bootstrap.php as
Kohana::modules(array(
'auth' => MODPATH.'auth', // Basic authentication
'rest' => MODPATH.'rest', // Basic Rest example
// 'cache' => MODPATH.'cache', // Caching with multiple backends
// 'codebench' => MODPATH.'codebench', // Benchmarking tool
'database' => MODPATH.'database', // Database access
// 'image' => MODPATH.'image', // Image manipulation
// 'minion' => MODPATH.'minion', // CLI Tasks
'orm' => MODPATH.'orm', // Object Relationship Mapping
// 'unittest' => MODPATH.'unittest', // Unit testing
// 'userguide' => MODPATH.'userguide', // User guide and API documentation
));
i have created a controller by extending "Controller_Rest" Now according to wiki i should be able to access "$this->_user, $this->_auth_type and $this->_auth_source" variables but in my case its not happening what i am doing wrong?
And i checked in console network it always showing status as "401 Unauthorised"
For using Authorization,you need to extend Kohana_RestUser Class
The module you are using comes with an abstract Kohana_RestUser class, which you must extend in your app. The only function that requires implementation is the protected function _find(). The function's implementation is expected to load any user related data, based on an API key.
I will explain you with an example
<?php
// Model/RestUser.php
class RestUser extends Kohana_RestUser {
protected $user='';
protected function _find()
{
//generally these are stored in databases
$api_keys=array('abc','123','testkey');
$users['abc']['name']='Harold Finch';
$users['abc']['roles']=array('admin','login');
$users['123']['name']='John Reese';
$users['123']['roles']=array('login');
$users['testkey']['name']='Fusco';
$users['testkey']['roles']=array('login');
foreach ($api_keys as $key => $value) {
if($value==$this->_api_key){
//the key is validated which is authorized key
$this->_id = $key;//if this not null then controller thinks it is validated
//$this->_id must be set if key is valid.
//setting name
$this->user = $users[$value];
$this->_roles = $users[$value]['roles'];
break;
}
}
}//end of _find
public function get_user()
{
return $this->name;
}
}//end of RestUser
Now Test Controller
<?php defined('SYSPATH') or die('No direct script access.');
//Controller/Test.php
class Controller_Test extends Controller_Rest
{
protected $_rest;
// saying the user must pass an API key.It is set according to the your requirement
protected $_auth_type = RestUser::AUTH_TYPE_APIKEY;
// saying the authorization data is expected to be found in the request's query parameters.
protected $_auth_source = RestUser::AUTH_SOURCE_GET;//depends on requirement/coding style
//note $this->_user is current Instance of RestUser Class
public function before()
{
parent::before();
//An extension of the base model class with user and ACL integration.
$this->_rest = Model_RestAPI::factory('RestUserData', $this->_user);
}
//Get API Request
public function action_index()
{
try
{
$user = $this->_user->get_name();
if ($user)
{
$this->rest_output( array(
'user'=>$user,
) );
}
else
{
return array(
'error'
);
}
}
catch (Kohana_HTTP_Exception $khe)
{
$this->_error($khe);
return;
}
catch (Kohana_Exception $e)
{
$this->_error('An internal error has occurred', 500);
throw $e;
}
}
//POST API Request
public function action_create()
{
//logic to create
try
{
//create is a method in RestUserData Model
$this->rest_output( $this->_rest->create( $this->_params ) );
}
catch (Kohana_HTTP_Exception $khe)
{
$this->_error($khe);
return;
}
catch (Kohana_Exception $e)
{
$this->_error('An internal error has occurred', 500);
throw $e;
}
}
//PUT API Request
public function action_update()
{
//logic to create
}
//DELETE API Request
public function action_delete()
{
//logic to create
}
}
Now RestUserData Model
<?php
//Model/RestUserData.php
class Model_RestUserData extends Model_RestAPI {
public function create($params)
{
//logic to store data in db
//You can access $this->_user here
}
}
So index.php/test?apiKey=abc returns
{
"user": {
"name": "Harold Finch",
"roles": [
"admin",
"login"
]
}
}
Note: K in apiKey is Capital/UpperCase
I Hope this Helps
Happy Coding :)

Symfony3: Service not able to get arguments

I have made a service to get Doctrine connection in my models (Not sure if it is a nice approach but I dont want to pass connection from controller to model constructor each time).
So lets say I want products in my controller
public function getProductsAction(Request $request) {
$product_model = new ProductModel();
return $product_model->getProducts();
}
I have Product model Which will access a helper to get "database_connection"
use AppBundle\Helper\ContainerHelper;
class ProductModel {
function getProducts() {
$helper = new ContainerHelper();
$db = $helper->getDoctrine();
$query = "SELECT * FROM customer_products;";
$statement = $db->prepare($query);
$statement->execute();
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
}
Now this helper is defined in src/AppBundle/Helper/ContainerHelper.php
namespace AppBundle\Helper;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
class ContainerHelper {
private $container;
public function __construct(Container $container) {
$this->container = $container;
}
public static function getDoctrine() {
$database_connection = $this->container->get('database_connection');
return $database_connection;
}
}
Lets say this service needs "service container" so in app/config/services.yml
services:
app.container_helper:
class: AppBundle\Helper\ContainerHelper
arguments: ['#service_container']
But it gives me error:
Catchable Fatal Error: Argument 1 passed to
AppBundle\Helper\ContainerHelper::__construct() must implement
interface Symfony\Component\DependencyInjection\ContainerInterface,
none given, called in \src\AppBundle\Model\ProductModel.php
on line 148 and defined
While I believe that I have implemented it correctly according to http://symfony.com/doc/current/book/service_container.html and http://anjanasilva.com/blog/injecting-services-in-symfony-2/, its certain that I have missed something or just got the whole bad idea. I need to know if it is a correct concept or what I have missed
While #pavlovich is trying to fix your existing code, I really think you are making this much more convoluted than it has to be. ProductModel itself should be a service with your database connection injected into it.
class ProductModel {
public function __construct($conn) {
$this->conn = $conn;
}
public function getProducts() {
$stmt = $this->conn->executeQuery('SELECT * FROM customer_products');
return $stmt->fetchAll();
}
services:
product_model:
class: AppBundle\...\ProductModel
arguments: ['#database_connection']
// controller.php
$productModel = $this->get('product_model'); // Pull from container
$products = $productModel->getProducts();
Rather than using helpers, I'd recommend using constructor injection and autowiring. It's more safe, future proof and easier to extend and test.
In such case, you'd have to create ProductRepository (more common and standard name for ProductModel) and pass it to controller.
1. Controller
<?php
class SomeController
{
/**
* #var ProductRepository
*/
private $productRepository;
public function __construct(ProductRepository $productRepository)
{
$this->productRepository = $productRepository;
}
public function getProductsAction()
{
return $this->productRepository->getProducts();
}
}
If you have difficulties to register controller as a service, just use Symplify\ControllerAutowire bundle.
2. ProductRepository
// src/AppBundle/Repository/ProductRepository.php
namespace AppBundle\Repository;
class ProductRepository
{
/**
* #var Doctrine\DBAL\Connection
*/
private $connection;
public function __construct(Doctrine\DBAL\Connection $connection)
{
$this->connection = $connection;
}
public function fetchAll()
{
$query = "SELECT * FROM customer_products;";
$statement = $this->connection->prepare($query);
$statement->execute();
return $statement->fetchAll(PDO::FETCH_ASSOC);
}
}
3. Service registration
# app/cofig/servies.yml
services:
product_repository:
class: AppBundle\Repository\ProductRepository
autowire: true
For more you can see similar question with answer here: Symfony 3 - Outsourcing Controller Code into Service Layer
With new version of Symfony 3.3, a new feature is added (Auto-wired Services Dependencies)
https://symfony.com/doc/current/service_container/autowiring.html
https://symfony.com/doc/current/service_container/3.3-di-changes.html
Using this feature, I solved this issue in following way:
Added a new directory /src/AppBundle/Model
Added my model classes in this directory
namespace AppBundle\Modal;
use Doctrine\ORM\EntityManagerInterface;
class ProductModal
{
private $em;
// We need to inject this variables later.
public function __construct(EntityManagerInterface $entityManager)
{
$this->em = $entityManager;
}
// We need to inject this variables later.
public function getProducts()
{
$statement = $this->em->getConnection()->prepare("SELECT * FROM product WHERE 1");
$statement->execute();
$results = $statement->fetchAll();
return $results;
}
}
Added in my app/config/services.yml
AppBundle\Modal\:
resource: '../../src/AppBundle/Modal/*'
public: true
In my controller I can use it like
$products = $this->get(ProductModal::class)->getProducts();
P.S. Dont forget to add use AppBundle\Entity\Product\Product; in controller

Yii2 rest create - saving to DB

I am trying to save data from the form using yii2 rest api controller, standard create action. New line is created in DB, and id is returned, but data from POST is not saved - only zeroes appear in DB, then I overrided create action by my own one, the same situation. But if I directly save to DB, without rest api controller, data is saved successfully.
What can be a reason of such strange saving to DB? Thanks!
By the way, in index I can see post data in the format: {"_csrf":"wergferw","table_name":{"sum":25000,"currency":1}}
Controller is very simple:
namespace frontend\controllers;
use yii;
use yii\rest\ActiveController;
use yii\web\Response;
class DemandController extends ActiveController
{
public $modelClass = 'frontend\models\Demands';
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['contentNegotiator']['formats']['application/json']
= Response::FORMAT_JSON;
return $behaviors;
}
}
I get reply in rest:
<response><id>37</id></response>
Model is generated by gii
namespace frontend\models;
use Yii;
class Demands extends \yii\db\ActiveRecord
{
public static function tableName()
{
return 'demands';
}
public function rules()
{
return [
[['sum', 'currency'], 'required'],
];
}
public function attributeLabels()
{
return [
'sum' => 'Sum',
'currency' => 'Currency',
];
}
}
If none of the posted data appears in the database, it sounds like a post format issue to me. Check that your HTTP Post headers have "Content-Type: application/x-www-form-urlencoded".
I am also having the problem so after changing this line it's worked for me..
You try to change the model->load() method inside yii/rest/createAction
public function run()
{
$model->load(Yii::$app->getRequest()->getBodyParams(), '');
change this line to
$model->load(Yii::$app->getRequest()->getBodyParams());
}
see this link for more info - http://www.yiiframework.com/doc-2.0/yii-base-model.html#load()-detail
The model->load($data,$formName) - the $data should be $_GET or $_POST value of array and $formName use to load the data into the model. If not set, formName() is used. so u have to change the model->load() in createAction class.
Better you have to override the activecontroller default create action then try
class CabController extends ActiveController
{
public $modelClass = 'api\modules\v1\models\Cab';
public function actions(){
$actions = parent::actions();
unset($actions['create']);
unset($actions['update']);
unset($actions['index']);
return $actions;
}
/* Declare methods supported by APIs */
protected function verbs(){
return [
'create' => ['POST'],
'update' => ['PUT', 'PATCH','POST'],
'delete' => ['DELETE'],
'view' => ['GET'],
'index'=>['GET'],
];
}
public function actionCreate(){
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$model = new Cab;
$post_data = Yii::$app->request->post();
$model->load($post_data);
$model->save(false);
return $model;
}
Include safe columns in your model
public function rules()
{
return [
[['sum', 'currency'], 'safe'],
];
}

Laravel redirect to post method

To stay basic I would like to create a bookmark app
I have a simple bookmarklet
javascript:location.href='http://zas.dev/add?url='+encodeURIComponent(location.href)
I created a rest controller
<?php
use zas\Repositories\DbLinkRepository;
class LinksController extends BaseController {
protected $link;
function __construct(DbLinkRepository $link) {
$this->link=$link;
// ...
//$this->beforeFilter('auth.basic', array('except' => array('index', 'show', 'store')));
// ...
}
public function index()
{
//return Redirect::to('home');
}
public function create()
{
}
public function store()
{
return 'hello';
//$this->link->addLink(Input::get('url'));
//return Redirect::to(Input::get('url'));
}
public function show($id)
{
//$url = $this->link->getUrl($id);
//return Redirect::to($url);
}
public function edit($id)
{
}
public function update($id){
}
public function destroy($id){
}
}
in the routes.php, I created a ressource
Route::resource('links','LinksController');
and as I want to redirect /add to the store method I added
Route::get('/add',function(){
return Redirect::action('LinksController#store');
});
but it never display the hello message, in place it redirects me to
http://zas.dev/links
I also tried with
return Redirect::route('links.store');
without much success
thanks for your help
Ok I now get what you are trying to do. This will work:
Route::get('add', 'LinksController#store');
Remove:
Route::resource('links','LinksController');
and remove:
Route::get('/add',function(){
return Redirect::action('LinksController#store');
});
Sorry it took so long!
The problem is that once you Redirect::, you loose all the Input values, so you should manually give them to your controller when you do the redirect, like so :
Redirect::route('links.store', ["url" => Input::get("url")]);
Finally add an $url parameter to your store method to receive the value we give it in the previous method, like this :
public function store($url) {
$this->link->addLink($url);
return Redirect::to($url);
}