Getting error while setting Slug in laravel --- profile%20/%20 - laravel

My li link to profile
<li>Profile</li>
My route file
Route::get('/profile/{slug}','ProfileController#index');
My profilecontroller page
class ProfileController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index($slug){
return view('profile.index');
}
}

Remove Space
<li>Profile</li>

Create a name for every route like:
Route::get('/profile/{slug}','ProfileController#index')->name('profile');
Then you can call this like:
<li>
Profile
</li>
If you don't want to use route name. You can do this like:
<li>
Profile
</li>

You need to go to your model and add this function
public function getRouteKeyName()
{
return 'slug';
}
This will instruct laravel to use "slug" as the router key, instead of ID

Related

How to architect a project which has more number of dynamic pages in laravel

I have a project which has a large number of dynamic pages. Approximately 30+ pages. The content of each page is different. what I did is created 30 tables and 30 routes as well. And on the admin side, there are 30+ modules to edit the contents. Is it the right way to do this?. In database table, different columns has to be kept.
// Route definition...
Route::get('/page1', [Page1Controller::class, 'show']);
Route::get('/page2', [Page2Controller::class, 'show']);
Route::get('/page3', [Page3Controller::class, 'show']);
// Controller method definition...
public function index() {
return view('page1');
}
// Route definition...
// All other routes above this slug catch all. otherwise it will try and hit this controller all the time and fail.
Route::get('/{slug}', [PageController::class, 'show']);
// Controller method definition...
public function show($slug)
{
$contents = PageContents::where('slug', $slug)->firstOrFail();
if ($contents) {
return view('page')->with('contents', $contents);
}
return view('404');
}
This way you have a table with all the contents you need. e.g. title, body copy so on. but if each layout is different you could also return a different view. e.g.
public function show($slug)
{
$contents = PageContents::where('slug', $slug)->firstOrFail();
if ($contents) {
return view('page-'.$slug)->with('contents', $contents);
}
return view('404');
}
You can create only one controller and add a parameter in the route(web.php):
web.php
//---Route Definition
Route::get('page/{page_number}', [PageController::class, 'show'])->name('page.show');
PageController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Crypt;
class PageController extends Controller {
//---Show
public function show($page_number) {
return view('show.show', compact('page_number'));
}//---End of Function show
}
If you want to retrieve your data from a database just one table is enough, just create a page table and give a field by the name of page_number and retrieve your specific page data by the given field.
For example:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Crypt;
class PageController extends Controller {
//---Show
public function show($page_number) {
$page = PageContent::where('page_number', $page_number)->first();
return view('show.show', compact('page'));
}//---End of Function show
}
**
Your Link to routes
<a href="{{ route('page.show', ['page_number' => 1])) }}" class="" title="Show">
Show
</a>

Laravel blade file can't view

I created CustomerController in Http, later, I fixed-route get customers, but getting an error in a single action Controller.
I tried to show off CustomerController view for displaying customers logged in page
Here is my error message:
Use of undefined constant view - assumed 'view' (this will throw an Error in a future version of PHP)
Looks like you're trying to access the old ways to render blade file look at this :-
return View::make('customers.index', $customersList);
To use view() method
return view('admin.pages.customers.index',compact('someVaiable'));
OR
// You can define constant for your controller get methods
private $layout;
public function __construct()
{
$this->layout = 'admin.pages.customers.';
}
public function index(){
return view($this->layout.'index');
}
Take a look a this for Single Action Controllers example
https://laravel.com/docs/5.8/controllers#single-action-controllers
The first argument of the view method in the controller should be the name of the view.
Routes/web.php
Route::get('/', 'CustomerController');
app/Http/Controllers/CustomerController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class CustomerController extends Controller
{
public function __invoke(Request $request)
{
return view('customers');
}
}
resources/views/customers.blade.php
<h1>Customers</h1>

index view isn't showing

hi I have created a new default index view but it not loading its content and showing the blank page,,, my file structure https://ibb.co/wW337xS
index blade:
#extends('layouts.frontLayout.front_design')
#section('content')
<!--html here-->
#endsection
controller:
<?php
namespace App\Http\Controllers;
use App\Index;
use Illuminate\Http\Request;
class IndexController extends Controller
{
public function index()
{
return view('index');
}
route:
Route::resource('/','IndexController');
Its problem is occured, becouse you send user to the view with name of 'index', it means that there is a blade page in the view folder of your resource, but as i see in your structure, the index.blade.php is in this address: layouts.index. then you can refactor your controller:
public function index()
{
return view('layouts.index');
}
wrong
Route::resource('/','IndexController');
right
Route::get('/', 'IndexController#index');

Getting error in Laravel 5.7 edit route page not found

Laravel Version 5.7
PHP 7+
I created a resource controller -> CategoryController [having all the magic methods]
This is the routes/web.php
Route::group(['as'=>'admin.','middleware'=>['auth','admin'],'prefix'=>'admin'], function(){
Route::get('/dashboard','AdminController#dashboard')->name('dashboard');
// product resource controller methods
// check php artisan r:l
Route::resource('product', 'ProductController');
Route::resource('category', 'CategoryController');
Route::resource('profile', 'ProfileController');
Route::post('remove', 'CategoryController#remove')->name('category.remove');
});
Now as you can see, I have "http://127.0.0.1:8000/admin/category/1/edit" for one of my categories to edit with category id = 1, that is also stored in the database.
<?php
namespace App\Http\Controllers;
use App\Category;
use Illuminate\Http\Request;
class CategoryController extends Controller
{
public function index()
{
$categories = Category::paginate(3);
return view('admin.categories.index',compact('categories'));
}
public function edit(Category $category)
{
return "This is category edit page";
// dd($category);
// $categories = Category::where('id','!=', $category->id)->get();
// // dd($categories);
// return "This is category edit page";
// return view('admin.categories.create',['categories' => $categories, 'category'=>$category]);
}
When I try to go to this edit category page, it shows 404 page not found error.
Although, when I made an individual route for edit method with a closure function to return some text, it worked perfectly.
Route::get('category/{category}/edit', function($category){
return $category;
})->name('category.edit');
You didn't excluded full error you get, but try to change:
public function edit(Category $category)
{
return "This is category edit page";
}
into:
public function edit($category)
{
return "This is category edit page";
}
and see if it helps. If it helps, it means that there is no record matching id you passed or this record is soft deleted (or some additional conditions are not met) - Laravel uses Route model binding to match valid record.
try this
public function edit(Request $category)
{
return "This is category edit page";
}

How to create login / registerform template with auth_tank/codeigniter?

Struggling to figure out how to achieve this. I want a login/registerform on same page when using tank_auth in CodeIgniter.
I was thinking of having something like this in my members controller (which extends My_Controller which extends Auth).
class Members extends My_Controller (
public function login()
{
//Already loggedin, do nothing
if ($this->tank_auth->is_logged_in()) {
return;
}
$this->view_data['login_by_username'] = true;
$this->view_data['login_by_email'] = false;
$this->view_data['show_captcha'] = false;
$this->view_data['login_form'] = $this->load->view('auth/login_form', $this->view_data, TRUE);
$this->view_data['register_form'] = $this->load->view('auth/register_form', $this->view_data, TRUE);
$this->v('members/login_register'); //In this view $login_form and $register_form is rendered
}
}
When I go to that page (localhost/members/login) the members/login-view is shown as expected, but in the action of the form for loginform it says:
http://localhost/members/login
Therefore when I click "Login-button" then it just calls members/login and that's not what I want. I want the action to be auth/login, but I still want to use my own template.
I hope you guys understand what I mean.... Please tell me if I'm doing anything wrong/thinking of this incorrectly.
UPATE:
In the actual template (members/login_register) it looks like this:
<div class="column">
<?php echo $login_form;?>
</div>
<div class="column>
<?php echo $register_form;?>
</div>
Maybe what I want to achieve is not possible?
Master View
You should use a Master view as a wrapper for your content.
That way you can easily pass in partial views(although not required)
However it will keep things neat. It also allows for easier control over
Admin/User Dashboards and your frontend.
Main Controller
class MY_Controller extends CI_Controller
{
public $template;
public function __construct()
{
$this->template = "master/template"; //views/master/template
}
}
To change it for an admin template(as an example of flexibility) you simply need to change it
the variable in the __constructor
class Admin_Controller extends MY_Controller
{
public function __construct()
{
$this->template = "master/admin/template"; //views/master/admin/template
}
}
Partial View(s)
Partial views can be used directly in other views.
You don't need a controller to call them.
These views are just stored in the buffer when they are
loaded by the loader class $this->load->view('', '', true)
A common approach is to create a folder inside /views called "partials".
This is where you would keep all you re-usable views(such as forms/widgets).
The Views(/views/partials/login_form)
<div>
<?php echo form_open('members/login', array('id'=>'login-form'))
</div>
The Views(/views/partials/signup_form)
<div>
<?php echo form_open('members/signup', array('id'=>'signup-form'))
</div>
The Views(members/index)
You can then combine the views
<div class="signup-form">
<?php echo $this->load->view('partials/signup_form')
</div>
<div class="login-form">
<?php echo $this->load->view('partials/login_form')
</div>
Login / Signup
In your members class you can create one method to show
the register/login form and then create methods to handle each of them individually.
class Members extends MY_Controller
{
public function index()
{
return $this->load->view('members/login_register');
}
public function login()
{
if(!$this->form_validation->run()){
return $this->index();
}
// form validation passed Ask Tank Auth
// to log the user in
$tank->auth->login();
}
public function signup()
{
if(!$this->form_validation->run()){
return $this->index();
}
// form validation passed Ask Tank Auth
// to register
$tank->auth->register();
}
}

Resources